python - Extracting column labels of the cells meeting a given condition -


suppose data @ hand in following form:

import pandas pd df = pd.dataframe({'a':[1,10,20], 'b':[4,40,50], 'c':[10,11,12]}) 

i can compute minimum row with:

df.min(axis=1) 

which returns 1 10 12.

instead of values, create pandas series containing column labels of corresponding cells.

that is, a c.

thanks suggestions.

you can use idxmin(axis=1) method:

in [8]: df.min(axis=1) out[8]: 0     1 1    10 2    12 dtype: int64  in [9]: df.idxmin(axis=1) out[9]: 0    1    2    c dtype: object  in [11]: df.idxmin(axis=1).values out[11]: array(['a', 'a', 'c'], dtype=object) 

Comments

Popular posts from this blog

javascript - Thinglink image not visible until browser resize -

firebird - Error "invalid transaction handle (expecting explicit transaction start)" executing script from Delphi -

mongodb - How to keep track of users making Stripe Payments -