简体   繁体   中英

create column of column heading with greatest value pandas python

I am struggling to describe, let alone attempt this. If I have a very basic table such as :

column1 value1  value2  value3
a   1   5   8
b   2   4   4
c   3   3   5
d   4   2   2
e   5   1   3

I want to add another column such as df[max_value] that will look at a set of columns and pick the column with the highest value along the row. Ie, the table would look this:

column1 value1  value2  value3 max_value
a   1   5   8 value3
b   2   4   4 value2/value3
c   3   3   5 value3
d   4   2   2 value1
e   5   1   3 value1

I am also unsure how to deal with instances where the value is equal across two columsn (such as row c).

A slight mod of my answer :

In [69]:
df['max_value'] = df.ix[:,1:].apply( lambda x: '/'.join(df.ix[:,1:].columns[x == x.max()]), axis=1)
df

Out[69]:
  column1  value1  value2  value3      max_value
0       a       1       5       8         value3
1       b       2       4       4  value2/value3
2       c       3       3       5         value3
3       d       4       2       2         value1
4       e       5       1       3         value1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM