Get column names for max values over a certain row in a pandas DataFrame

后端 未结 2 651
予麋鹿
予麋鹿 2021-01-14 09:39

In the DataFrame

import pandas as pd 
df=pd.DataFrame({\'col1\':[1,2,3],\'col2\':[3,2,1],\'col3\':[1,1,1]},index= [\'row1\',\'row2\',\'row3\'])
print df
             


        
2条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-14 10:19

    you could also use apply and create a method such has:

    def returncolname(row, colnames):
        return colnames[np.argmax(row.values)]
    
    df['colmax'] = df.apply(lambda x: returncolname(x, df.columns), axis=1)
    
    Out[62]: 
    row1    col2
    row2    col1
    row3    col1
    dtype: object
    

    an you can use df.max(axis=1) to extract maxes

    df.max(axis=1)
    Out[69]: 
    row1    3
    row2    2
    row3    3
    

提交回复
热议问题