pandas数组获取最大值索引的方法-argmax和idxmax

 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

 

posted @ 2018-06-22 00:10  诗&远方  阅读(43454)  评论(1编辑  收藏  举报