TransWikia.com

Pandas dataframe only select the columns that have all True

Stack Overflow Asked by learningisfun on December 11, 2020

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]

2 Answers

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)

Answered by ericl16384 on December 11, 2020

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

df.columns[df.all()]

Output:

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

Answered by Quang Hoang on December 11, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP