Series的idxmax和argmax

转载至:https://www.cnblogs.com/liulangmao/p/9211537.html

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

举个栗子:

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

Series的idxmax和argmax
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)
Series的idxmax和argmax

可以这样做:

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
上一篇:如何继续执行之前失败的maven build


下一篇:maven工具日常开发常用命令