如何獲取 Pandas Series 中最大值的索引位置?
在 Pandas Series 建構函式中,有一個名為 argmax() 的方法,用於獲取 Series 資料中最大值的索引位置。
Pandas Series 是一種一維資料結構物件,具有行索引值。透過使用行索引值,我們可以訪問資料。
Pandas Series 中的 argmax() 方法用於獲取 Series 物件最大值的索引位置。argmax 方法的輸出是一個整數值,表示最大值所在的位置。
示例 1
# import pandas package
import pandas as pd
import numpy as np
# create a pandas series
s = pd.Series(np.random.randint(10,100, 10))
print(s)
# Apply argmax function
print('Output of argmax', s.argmax())解釋
讓我們建立一個包含 10 個 10 到 100 範圍內的隨機整數值的 Pandas Series 物件,並應用 argmax() 函式來獲取 Series 值中最大值的索引位置。
輸出
0 81 1 94 2 75 3 13 4 17 5 42 6 45 7 29 8 25 9 59 dtype: int32 Output of argmax: 1
對於以下示例,Pandas Series 物件的 argmax() 方法返回了一個整數值“1”,該整數值表示給定 Series 元素中最大值的索引位置。
示例 2
import pandas as pd
import numpy as np
# creating pandas Series object
series = pd.Series({'Black':10, 'White':29,'Red':82, 'Blue':56,'Green':67})
print(series)
# Apply argmax function
print('Output of argmax:',series.argmax())解釋
在以下示例中,我們使用 Python 字典建立了一個 pandas.Series 物件“series”,生成的 Series 具有命名索引標籤和整數資料。之後,我們對 Series 物件的資料應用了 argmax() 方法以獲取最大數字的索引位置。
輸出
Black 10 White 29 Red 82 Blue 56 Green 67 dtype: int64 Output of argmax: 2
對於以下示例,argmax() 方法的輸出為 2,這意味著索引標籤“Red”處的值具有最大值。
如果最大值在多個位置出現,則 argmax 方法將返回第一個行位置作為輸出。
廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP