Given a dataframe df
, I'd apply some condition df[condition]
and retrieve a subset. I just want to check if there are any rows in the subset - this would tell me the condition is a valid one.
In [551]: df
Out[551]:
Col1
0 1
1 2
2 3
3 4
4 5
5 3
6 1
7 2
8 3
What I want to check is something like this:
if df[condition] has rows:
do something
What is the best way to check whether a filtered dataframe has rows? Here's some methods that don't work:
if df[df.Col1 == 1]:
GivesValueError: The truth value of a DataFrame is ambiguous.
if df[df.Col1 == 1].any():
Also givesValueError
I suppose I can test the len
. Are there other ways?