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