[英]Pandas Dataframe: Find the index of maximum value for each column using idxmax()
I have a dataframe 156 x 9 with no null values and I am trying to find the index of the maximum value of each column.我有一个 dataframe 156 x 9 没有 null 值,我试图找到每列最大值的索引。 I tried the following
我尝试了以下
country_list = []
keys = []
for i in range(len(col)):
country = df.loc[df[col[i]].idxmax()]['Country or region']
country_list.append(country)
keys.append(col[i])
The columns available are可用的列是
In[65]:df.columns
Out[65]:
Index(['Overall rank', 'Country or region', 'Score', 'GDP per capita',
'Social support', 'Healthy life expectancy',
'Freedom to make life choices', 'Generosity',
'Perceptions of corruption'],
dtype='object')
But I got the error of for using idxmax TypeError: reduction operation 'argmax' not allowed for this dtype
但是我得到了使用 idxmax
TypeError: reduction operation 'argmax' not allowed for this dtype
Any help appreciated, thank you!任何帮助表示赞赏,谢谢!
Error means there are some non numeric column(s).错误意味着有一些非数字列。
Select only numeric columns by DataFrame.select_dtypes
and then use DataFrame.idxmax
: Select 仅按
DataFrame.select_dtypes
的数字列,然后使用DataFrame.idxmax
:
df = pd.DataFrame({
'A':list('abcdef'),
'B':[4,5,4,5,5,4],
'C':[7,8,9,4,2,3],
'D':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'F':list('aaabbb')
})
print (df)
A B C D E F
0 a 4 7 1 5 a
1 b 5 8 3 3 a
2 c 4 9 5 6 a
3 d 5 4 7 9 b
4 e 5 2 1 2 b
5 f 4 3 0 4 b
df1 = df.select_dtypes(np.number).idxmax()
print (df1)
B 1
C 2
D 3
E 3
dtype: int64
You can try this:-你可以试试这个: -
import numpy as np
col_names = df.columns
max_val_index = []
for col in col_names:
max_val_index.append(np.argmax(df[col]))
The max index value will be stored in max_val_index最大索引值将存储在 max_val_index
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.