Python pandas check if dataframe is not empty Python pandas check if dataframe is not empty python-3.x python-3.x

Python pandas check if dataframe is not empty


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.


.empty returns a boolean value

>>> df_empty.emptyTrue

So if not empty can be written as

if not df.empty:    #Your code

Check pandas.DataFrame.empty , might help someone.


You can use the attribute dataframe.empty to check whether it's empty or not:

if not dataframe.empty:    #do something

Or

if len(dataframe) != 0:   #do something

Or

if len(dataframe.index) != 0:   #do something