Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

pandas

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
       col1  col2  col3
row1     1     3     1
row2     2     2     1
row3     3     1     1

I want to get the column names of the cells with the max value(s) over a certain row.

The desired output would be (in pseudocode):

get_column_name_for_max_values_of(row2)
>['col1','col2']

What would be the most concise way to express

get_column_name_for_max_values_of(row2)

?

like image 492
user1934212 Avatar asked Oct 24 '25 13:10

user1934212


2 Answers

If not duplicates, you can use idxmax, but it return only first column of max value:

print (df.idxmax(1))
row1    col2
row2    col1
row3    col1
dtype: object

def get_column_name_for_max_values_of(row):
    return df.idxmax(1).ix[row]

print (get_column_name_for_max_values_of('row2'))
col1

But with duplicates use boolean indexing:

print (df.ix['row2'] == df.ix['row2'].max())
col1     True
col2     True
col3    False
Name: row2, dtype: bool

print (df.ix[:,df.ix['row2'] == df.ix['row2'].max()])
      col1  col2
row1     1     3
row2     2     2
row3     3     1

print (df.ix[:,df.ix['row2'] == df.ix['row2'].max()].columns)
Index(['col1', 'col2'], dtype='object')

And function is:

def get_column_name_for_max_values_of(row):
    return df.ix[:,df.ix[row] == df.ix[row].max()].columns.tolist()

print (get_column_name_for_max_values_of('row2'))
['col1', 'col2']
like image 120
jezrael Avatar answered Oct 26 '25 02:10

jezrael


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
like image 31
Steven G Avatar answered Oct 26 '25 04:10

Steven G



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!