learningisfun
learningisfun

Reputation: 181

Pandas dataframe only select the columns that have all True

Given a dataframe df, I need to select the columns that have only True values

df = 
      A      B      C       D       E
    True    False   True   False   True

Output should be

output = [A, C, E]

Upvotes: 2

Views: 1757

Answers (2)

ericl16384
ericl16384

Reputation: 364

Try iterating through it and putting the keys in a list (you can easily modify this to result in a dict, though).

result = []
for i in df.keys():
    if df[i].all():
        result.append(i)

Upvotes: 1

Quang Hoang
Quang Hoang

Reputation: 150765

Try boolean indexing with all (for only True values):

df.columns[df.all()]

Output:

Index(['A', 'C', 'E'], dtype='object')

Upvotes: 4

Related Questions