Column Label Of Max In Pandas
I am trying to extract maximum value in row and contributing column label from pandas dataframe. For example, A B C D index x 0 1 2 3 y 3 2 1 0 I expect the follow
Solution 1:
Need axis='columns'
or axis=1
in DataFrame.idxmax
:
df['Con'] = df.idxmax(axis='columns')
print (df)
A B C D Maxv Con
index
x 0 1 2 3 3 D
y 3 2 1 0 3 A
Or:
df['Con'] = df.idxmax(axis=1)
print (df)
A B C D Maxv Con
index
x 0 1 2 3 3 D
y 3 2 1 0 3 A
You get NaN
s, because data are not align to index
:
print (df.idxmax(axis='rows'))
A y
B y
C x
D x
dtype: object
Post a Comment for "Column Label Of Max In Pandas"