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:

  1. if df[df.col1 == 1]: gives valueerror: truth value of dataframe ambiguous.

  2. if df[df.col1 == 1].any(): gives valueerror

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

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -