How to check if a pandas dataframe is empty

Overview

The empty property of the DataFrame class indicates whether a given dataframe is empty. It is a boolean property, meaning it returns True if the dataframe is empty. NaNs values are considered to be valid entries in the dataframe, and hence if the data frame contains only NaNs, it is not considered empty.

Note: Refer to What is pandas in Python to learn more about pandas.

Syntax

Dataframe.empty

Example

import pandas as pd
import numpy as np
df = pd.DataFrame({"col1" : []})
print(df)
print("Is the above dataframe empty? %s" % (df.empty, ))
print("-" * 5)
df = pd.DataFrame({"col1" : [np.nan], "col2" : [np.nan]})
print(df)
print("Is the above dataframe empty? %s" % (df.empty, ))

Explanation

  • Lines 1–2: We import the pandas and numpy modules.
  • Line 4: We define an empty dataframe, df.
  • Lines 5–6: We print the value of the empty attribute on df.
  • Line 9: We assign a new data frame with NaNs to the df dataframe.
  • Lines 5–6: We print the value of the empty attribute on df.

Free Resources