107

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?

neuro
  • 14,948
  • 3
  • 36
  • 59
Arthur Zangiev
  • 1,438
  • 3
  • 14
  • 21
  • 1
    Why can't you do `not dataframe.empty`? – EdChum Apr 11 '16 at 08:34
  • `not df.empty` or a faster `len(df.index)` check? – Zero Apr 11 '16 at 08:35
  • @Zero, can anyone speak to the preferability of `len(df.index)` vs `df.empty`? pylint prefers the latter, but do performance considerations outweigh the stylistic benefit (if it exists)? – 3pitt Jan 24 '18 at 16:57

5 Answers5

167

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.

Akshat Mahajan
  • 9,543
  • 4
  • 35
  • 44
18

.empty returns a boolean value

>>> df_empty.empty
True

So if not empty can be written as

if not df.empty:
    #Your code

Check pandas.DataFrame.empty , might help someone.

Santhosh
  • 1,554
  • 1
  • 13
  • 19
16

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
Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
1

As already clearly explained by other commentators, you can negate a boolean expression in Python by simply prepending the not operator, hence:

if not df.empty:
  # do something

does the trick.

I only want to clarify the meaning of "empty" in this context, because it was a bit confusing for me at first.

According to the Pandas documentation, the DataFrame.empty method returns True if any of the axes in the DataFrame are of length 0.

As a consequence, "empty" doesn't mean zero rows and zero columns, like someone might expect. A dataframe with zero rows (axis 1 is empty) but non-zero columns (axis 2 is not empty) is still considered empty:

> df = pd.DataFrame(columns=["A", "B", "C"])
> df.empty
True

Another interesting point highlighted in the documentation is a DataFrame that only contains NaNs is not considered empty.

> df = pd.DataFrame(columns=["A", "B", "C"], index=['a', 'b', 'c'])
> df
     A    B    C
a  NaN  NaN  NaN
b  NaN  NaN  NaN
c  NaN  NaN  NaN
> df.empty
False
Sal Borrelli
  • 2,201
  • 19
  • 19
-2

Another way:

if dataframe.empty == False:
    #do something`
Arjun
  • 85
  • 7