AnswerBun.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!

Related Questions

How to Deserialize a list of objects from json in flutter

8  Asked on January 28, 2021 by gainz

     

How to pass input variable in structure directive?

1  Asked on January 27, 2021 by alice-messis

 

How to iterate over a list of floats in python

4  Asked on January 27, 2021 by joeyhoward988

 

Can I run docker system prune -a without downtime

1  Asked on January 27, 2021 by aswin-george

 

Calculating total Price of groceries with execptions in Python

4  Asked on January 27, 2021 by shemtheultimate

 

handling multiple useState inside conditional ternary

2  Asked on January 27, 2021 by kachi-cheong

     

react-native existing app crash on Android 4.1

0  Asked on January 27, 2021 by s-leg3ndz

     

HTML table widths set proportionally

1  Asked on January 27, 2021 by mayur-arora

   

Alsa issues on Raspberry pi

1  Asked on January 27, 2021 by suraj-hebbar-shankar

 

How to merge dictionaries with the same key and value in Python

1  Asked on January 26, 2021 by arbin-bulaybulay

 

AJAX switching HTML

1  Asked on January 26, 2021 by mimi

       

Using request to return a json value

1  Asked on January 26, 2021 by gta-sprx

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP