Reputation: 111
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. 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
Any help appreciated, thank you!
Upvotes: 1
Views: 1305
Reputation: 179
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
Upvotes: 0
Reputation: 862581
Error means there are some non numeric column(s).
Select only numeric columns by DataFrame.select_dtypes
and then use 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
Upvotes: 6