numpy,pandas求最大值索引

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值