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

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -