lundi 29 octobre 2018

Multiple check for empty dataframe

I have a situation where I need to move the dataframe forward in code only if it is not empty. Illustrated below:

----- Filter 1 -------
Check if df.empty then return emptydf
else
----- Filter 2 ------
Check if df.empty then return emptydf
else
----- Filter 3 ------
return df

The code for the above is written as below(Just a part of code):

df = df[df.somecolumn > 2].copy()

if df.empty:
    return df

df = df[df.someother == 2].copy()

if df.empty:
    return df

df = df[df.all <= 10].copy()

return df

If I have many such filters which expect dataframe not to be empty, I need to check empty after each filter. Is there any better way of checking dataframe empty rather than checking at each level.

Aucun commentaire:

Enregistrer un commentaire