python - Pandaic way to check whether a dataframe has any rows -
this question has answer here:
given dataframe df
, i'd apply condition df[condition]
, retrieve subset. want check if there rows in subset - tell me condition 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 want check this:
if df[condition] has rows:
what best way check whether filtered dataframe has rows? here's methods don't work:
if df[df.col1 == 1]:
givesvalueerror: truth value of dataframe ambiguous.
if df[df.col1 == 1].any():
givesvalueerror
i suppose can test len
. there other ways?
you use df.empty
:
df_conditional = df.loc[df['column_name'] == some_value] if not df_conditional.empty: ... # process dataframe results
Comments
Post a Comment