简体   繁体   中英

New column with column name from max column by index pandas

I want to create a new column with column name for the max value by index.

Tie would include both columns.

                A   B   C   D   
  TRDNumber                 
ALB2008081610   3   1   1   1   
ALB200808167    1   3   4   1   
ALB200808168    3   1   3   1   
ALB200808171    2   2   5   1   
ALB2008081710   1   2   2   5   

Desired output

                A   B   C   D   Best
  TRDNumber                 
ALB2008081610   3   1   1   1   A
ALB200808167    1   3   4   1   C
ALB200808168    3   1   3   1   A,C
ALB200808171    2   2   5   1   C
ALB2008081710   1   2   2   5   D

I have tried the following code

df.groupby(['TRDNumber'])[cols].max()

you can do:

>>> f = lambda r: ','.join(df.columns[r])
>>> df.eq(df.max(axis=1), axis=0).apply(f, axis=1)
TRDNumber
ALB2008081610      A
ALB200808167       C
ALB200808168     A,C
ALB200808171       C
ALB2008081710      D
dtype: object
>>> df['best'] = _
>>> df
               A  B  C  D best
TRDNumber                     
ALB2008081610  3  1  1  1    A
ALB200808167   1  3  4  1    C
ALB200808168   3  1  3  1  A,C
ALB200808171   2  2  5  1    C
ALB2008081710  1  2  2  5    D

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