My dataframe has mulitple columns. I find max value in each row. Now I want to know the which column is max in each row and I want to store it. Finally, I would like to know how many times this row was max (top), etc. My code:
df=
Aple Bana Oran
0 10 20 30
1 2 5 1
2 8 9 4
3 45 21 18
cols = df.columns.tolist
df['max_val'] = df[cols].max(axis=1)
df['max_col'] = df[df[cols]==df['max']] # store name of max value
Present output:
Above df['max_val']
produced the desired output. The problem is with the df['max_col']
. It failed to produced desired output
Expected output:
df=
Aple Bana Oran max_val max_col
0 10 20 30 30 Oran
1 2 5 1 5 Bana
2 8 9 4 9 Bana
3 45 21 18 45 Aple
print(df['max_col'].value_counts())
Bana 2
Oran 1
Aple 1