Austin
Austin

Reputation: 7329

Pandas: getting list of columns where max value == N

I know this way works, but is there a more standard way of doing this?

N=1
cols = list(data.iloc[:,(data.max()==1).values].columns)

Upvotes: 1

Views: 40

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

This will do the job:

df=pd.DataFrame({'a':[1,-1],'b':[3,4]})
df.columns[df.max()==1].tolist()

This prints ['a']

Upvotes: 1

Related Questions