numpy
numpy.argmax(a, axis=None, out=None)
如果数组有多个最大值,只会返回第一个的索引。
- axis int, optional
By default, the index is into the flattened array, otherwise along the specified axis.
如果不给axis传值,那么默认把数组拉平,返回拉平后的索引。
a=np.arange(10).reshape(5,2)
a[1,1]=9
print(a)
#array([[0, 1],
# [2, 9],
# [4, 5],
# [6, 7],
# [8, 9]])
np.argmax(a)#
# 3
np.argmax(a,axis=0)
#array([4, 1], dtype=int64),即a[0,4],a[1,1]
np.argmax(a,axis=1)
#array([1, 1, 1, 1, 1], dtype=int64)
pandas
Series.argmax(axis=None, skipna=True, *args, **kwargs)
返回数值索引
- Returns
int
Row position of the maximum value.
In [2]: import pandas as pd
In [3]: s = pd.Series({'Corn Flakes': 100.0, 'Almond Delight': 110.0,
...: 'Cinnamon Toast Crunch': 120.0, 'Cocoa Puff': 110.0})
In [4]: s
Out[4]:
Corn Flakes 100.0
Almond Delight 110.0
Cinnamon Toast Crunch 120.0
Cocoa Puff 110.0
dtype: float64
In [5]: s.argmax()
Out[5]: 2
DataFrame.idxmax(axis=0, skipna=True)
返回标签索引
- Parameters
axis {0 or ‘index’, 1 or ‘columns’}, default 0
The axis to use. 0 or ‘index’ for row-wise, 1 or ‘columns’ for column-wise.
In [6]: df = pd.DataFrame({'consumption': [10.51, 103.11, 55.48],
...: 'co2_emissions': [37.2, 19.66, 1712]},
...: index=['Pork', 'Wheat Products', 'Beef'])
In [7]: df
Out[7]:
consumption co2_emissions
Pork 10.51 37.20
Wheat Products 103.11 19.66
Beef 55.48 1712.00
In [8]: df.idxmax()
Out[8]:
consumption Wheat Products
co2_emissions Beef
dtype: object
In [9]: df.idxmax(axis="columns")
Out[9]:
Pork co2_emissions
Wheat Products consumption
Beef co2_emissions
dtype: object