Add Columns To Pandas Dataframe Containing Max Of Each Row, And Corresponding Column Name
My system Windows 7, 64 bit python 3.5.1 The challenge I've got a pandas dataframe, and I would like to know the maximum value for each row, and append that info as a new column. I
Solution 1:
You can compare the df against maxval
using eq
with axis=0
, then use apply
with a lambda
to produce a boolean mask to mask the columns and join
them:
In [183]:
df['maxcol'] = df.ix[:,:'c'].eq(df['maxval'], axis=0).apply(lambda x: ','.join(df.columns[:3][x==x.max()]),axis=1)
df
Out[183]:
a b c maxval maxcol
0 1 0 0 1 a
1 0 0 0 0 a,b,c
2 0 1 0 1 b
3 1 0 0 1 a
4 3 1 0 3 a
Post a Comment for "Add Columns To Pandas Dataframe Containing Max Of Each Row, And Corresponding Column Name"