Check if Pandas DataFrame is Empty

You can use df.empty to check if Pandas DataFrame is empty:

df.empty

Where:

  • True” means that the DataFrame is empty
  • False” means that the DataFrame is not empty

Steps to Check if Pandas DataFrame is Empty

Step 1: Create a DataFrame

To start with a simple example, let’s create a DataFrame with 2 columns:

import pandas as pd

data = {'Color': ['Blue', 'Blue', 'Green', 'Green', 'Red', 'Red'],
        'Height': [15, 20, 25, 20, 15, 25]
        }

df = pd.DataFrame(data)

print(df)

Run the code in Python, and you’ll see the following DataFrame:

   Color  Height
0   Blue      15
1   Blue      20
2  Green      25
3  Green      20
4    Red      15
5    Red      25

Step 2: Check if the DataFrame is Empty

Next, check if the DataFrame is empty by adding the following syntax:

df.empty

Therefore, the complete Python code would be:

import pandas as pd

data = {'Color': ['Blue', 'Blue', 'Green', 'Green', 'Red', 'Red'],
        'Height': [15, 20, 25, 20, 15, 25]
        }

df = pd.DataFrame(data)

print(df.empty)

Once you run the code, you’ll get “False” which means that the DataFrame is not empty:

False

Step 3: Drop all Columns in the DataFrame

Let’s now drop all the columns in the DataFrame using this syntax:

df.drop(['Color', 'Height'], axis=1, inplace=True)

Here is the complete Python code to drop all the columns, and then check if the DataFrame is empty:

import pandas as pd

data = {'Color': ['Blue', 'Blue', 'Green', 'Green', 'Red', 'Red'],
        'Height': [15, 20, 25, 20, 15, 25]
        }

df = pd.DataFrame(data)

df.drop(['Color', 'Height'], axis=1, inplace=True)

print(df.empty)

You’ll now get “True” which means that the DataFrame is Empty:

True

Dealing with NaNs

Let’s create a DataFrame that contains only NaN values:

import pandas as pd
import numpy as np

data = {'first_column': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
        'second_column': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]
        }

df = pd.DataFrame(data)

print(df)

As you can see, the DataFrame contains only NaNs:

   first_column  second_column
0           NaN            NaN
1           NaN            NaN
2           NaN            NaN
3           NaN            NaN
4           NaN            NaN
5           NaN            NaN

Next, check if the DataFrame is empty:

import pandas as pd
import numpy as np

data = {'first_column': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
        'second_column': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]
        }

df = pd.DataFrame(data)

print(df.empty)

You’ll get “False” which means that the DataFrame is not empty, even if it contains only NaNs:

False

You can drop the NaN values using dropna:

df.dropna(inplace=True)

Let’s drop all the NaN values in the DataFrame, and then check again if the DataFrame is empty:

import pandas as pd
import numpy as np

data = {'first_column': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
        'second_column': [np.nan, np.nan, np.nan, np.nan, np.nan, np.nan]
        }

df = pd.DataFrame(data)

df.dropna(inplace=True)

print(df.empty)

As you can see, the DataFrame is indeed empty after dropping the NaNs:

True

You can check the Pandas Documentation to learn more about df.empty.

Leave a Comment