pandas idxmax()函数

原文链接:https://blog.csdn.net/weixin_34227447/article/details/86130861
获取pandas中series最大值对应的索引。

 pandas Series 的 argmax 方法和 idxmax 方法用于获取 Series 的最大值的索引值:

举个栗子:

有一个pandas Series,它的索引是国家名,数据是就业率,要找出就业率最高的国家:

import pandas as pd

countries = [
‘Afghanistan’, ‘Albania’, ‘Algeria’, ‘Angola’,
‘Argentina’, ‘Armenia’, ‘Australia’, ‘Austria’,
‘Azerbaijan’, ‘Bahamas’, ‘Bahrain’, ‘Bangladesh’,
‘Barbados’, ‘Belarus’, ‘Belgium’, ‘Belize’,
‘Benin’, ‘Bhutan’, ‘Bolivia’, ‘Bosnia and Herzegovina’,
]

employment_values = [
55.70000076, 51.40000153, 50.5 , 75.69999695,
58.40000153, 40.09999847, 61.5 , 57.09999847,
60.90000153, 66.59999847, 60.40000153, 68.09999847,
66.90000153, 53.40000153, 48.59999847, 56.79999924,
71.59999847, 58.40000153, 70.40000153, 41.20000076,
]

# Employment data in 2007 for 20 countries
employment = pd.Series(employment_values, index=countries)

可以这样做:

max_country = employment.idxmax()     

max_country = employment.argxmax()

# 结果: ‘Angola’

 

如果是一个没有索引值的Series,则返回它的位置索引:

pure_employment = pd.Series(employment_values)
print(pure_employment.argmax())
print(pure_employment.idxmax())

# 结果: 3

 

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值