4

I have a dataframe df defined as below :

df = pd.DataFrame()
df["A"] = ['True','True','True','True','True']
df["B"] = ['True','False','True','False','True']
df["C"] = ['False','True','False','True','False']
df["D"] = ['True','True','False','False','False']
df["E"] = ['False','True','True','False','True']
df["F"] = ['HI','HI','HI','HI','HI']
>> df

      A      B      C      D      E   F
0  True   True  False   True  False  HI
1  True  False   True   True   True  HI
2  True   True  False  False   True  HI
3  True  False   True  False  False  HI
4  True   True  False  False   True  HI

and a list

lst = ["A","C"]

I would like to filter the rows in df based on the values being 'True' for the columns in lst. That is, I would like to get my resultant dataframe as :

      A      B     C      D      E   F
1  True  False  True   True   True  HI
3  True  False  True  False  False  HI

Instead of looping through the column names from the list and filtering it, is there any better solution for this?

4
  • 1
    +1 for the easy way of getting your dataframe. Most question put in their dataframe and if I want to help I need to define every column on my own. Commented Aug 7, 2020 at 13:57
  • 2
    @MisterNox you can use pd.read_clipboard('\s\s+') Commented Aug 7, 2020 at 14:22
  • That's a good one. I did not know this. Thanks for sharing. Commented Aug 7, 2020 at 14:25
  • it will be True False instead of 'True' 'False' in df Commented Aug 10, 2020 at 6:49

1 Answer 1

5

Use DatFrame.all over the column axis (axis=1):

df[df[lst].all(axis=1)]

      A      B     C      D      E   F
1  True  False  True   True   True  HI
3  True  False  True  False  False  HI

Details:

We get the columns in scope with df[lst], then we use all to check which rows have "all" as True:

df[lst].all(axis=1)

0    False
1     True
2    False
3     True
4    False
dtype: bool
Sign up to request clarification or add additional context in comments.

4 Comments

nice one, my mind went straight to df[df[lst].sum(1).eq(len(lst))] but this is more idiomatic and pythonic
@Erfan, thanks for the answer. I have a doubt. What if I need to compare each value with a string rather than checking whether it is True/False?
Please edit your input data in your question to showcase the problem, because right now it matches your expected output.
@Erfan, I have edited the input data in the question, can you please help me?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.