Python pandas check if dataframe is not empty

pandaspythonpython-3.x

I have an if statement where it checks if the data frame is not empty. The way I do it is the following:

if dataframe.empty:
    pass
else:
    #do something

But really I need:

if dataframe is not empty:
    #do something

My question is – is there a method .not_empty() to achieve this? I also wanted to ask if the second version is better in terms of performance? Otherwise maybe it makes sense for me to leave it as it is i.e. the first version?

Best Answer

Just do

if not dataframe.empty:
     # insert code here

The reason this works is because dataframe.empty returns True if dataframe is empty. To invert this, we can use the negation operator not, which flips True to False and vice-versa.