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)
?
you could also use apply and create a method such has:
an you can use df.max(axis=1) to extract maxes
If not duplicates, you can use
idxmax
, but it return only first column ofmax
value:But with duplicates use
boolean indexing
:And function is: