117

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?

3
  • 1
    Why can't you do not dataframe.empty? Commented Apr 11, 2016 at 8:34
  • not df.empty or a faster len(df.index) check? Commented Apr 11, 2016 at 8: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)? Commented Jan 24, 2018 at 16:57

5 Answers 5

178

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.

Sign up to request clarification or add additional context in comments.

Comments

21

.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.

Comments

19

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

3 Comments

or just if len(dataframe):
What about if len(dataframe) != 0:?
Might add if df.shape[0] also
5

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

Comments

-2

Another way:

if dataframe.empty == False:
    #do something`

1 Comment

Checking boolean values like this is an anti-pattern and should be avoided.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.