Reputation: 1025
For the dataframe df, I am selecting the rows that have True values either in column 'a' or 'b'.
>>> df
Out[127]:
a b
0 False False
1 True True
2 True False
>>> con = (df['a'] == True) | (df['b'] == True)
>>> con
Out[129]:
0 False
1 True
2 True
dtype: bool
>>> df[con]
Out[130]:
a b
1 True True
2 True False
There are only two columns in the dataframe. For the actual code, the number of such columns is a variable. How can the condition con
be generated on-the-fly?
Say, when df has 26 columns from a through z, I want something like
>>> con = (df['a'] == True) | (df['b'] == True) | ... (df['y'] == True) | (df['z'] == True)
which I can use to get the desired rows
Upvotes: 1
Views: 541
Reputation: 353549
You can use DataFrame.any
:
>>> df = pd.DataFrame(np.random.choice([True]+[False]*5, size=(6,5)), columns=list("abcde"))
>>> df
a b c d e
0 False False False False False
1 False False True False False
2 False False True False False
3 False False False False False
4 False False False False True
5 False False False False False
>>> df.any(axis=1)
0 False
1 True
2 True
3 False
4 True
5 False
dtype: bool
>>> df[df.any(axis=1)]
a b c d e
1 False False True False False
2 False False True False False
4 False False False False True
And as always you can use df.loc[df.any(axis=1)]
if you want to ensure you have a handle on the original.
Upvotes: 3