Pandas DataFrame.empty


Pandas DataFrame.empty

The DataFrame.empty property in pandas is used to check whether a DataFrame is empty or not. A DataFrame is considered empty if it has no entries (i.e., it has zero rows or columns).


Syntax

The syntax for accessing the empty property is:

DataFrame.empty

Here, DataFrame refers to the pandas DataFrame being checked for emptiness.


Returns

A boolean value:

  • True if the DataFrame is empty (no rows or columns).
  • False otherwise.

Examples

Checking if a DataFrame is Empty

You can use the empty property to check if a DataFrame has any data.

Python Program

import pandas as pd

# Create an empty DataFrame
df_empty = pd.DataFrame()

# Check if the DataFrame is empty
print("Is the DataFrame empty?")
print(df_empty.empty)

Output

Is the DataFrame empty?
True

Checking a Non-Empty DataFrame

If a DataFrame has data, the empty property will return False.

Python Program

import pandas as pd

# Create a DataFrame with data
data = {
    'Name': ['Arjun', 'Ram', 'Priya'],
    'Age': [25, 30, 35]
}
df = pd.DataFrame(data)

# Check if the DataFrame is empty
print("Is the DataFrame empty?")
print(df.empty)

Output

Is the DataFrame empty?
False

Handling a DataFrame with No Rows

A DataFrame with columns but no rows is also considered empty.

Python Program

import pandas as pd

# Create a DataFrame with no rows
data = {
    'Name': [],
    'Age': []
}
df_no_rows = pd.DataFrame(data)

# Check if the DataFrame is empty
print("Is the DataFrame empty (no rows)?")
print(df_no_rows.empty)

Output

Is the DataFrame empty (no rows)?
True

Handling a DataFrame with No Columns

A DataFrame with rows but no columns is also considered empty.

Python Program

import pandas as pd

# Create a DataFrame with no columns
df_no_columns = pd.DataFrame(index=[0, 1, 2])

# Check if the DataFrame is empty
print("Is the DataFrame empty (no columns)?")
print(df_no_columns.empty)

Output

Is the DataFrame empty (no columns)?
True

Summary

In this tutorial, we explored the DataFrame.empty property in pandas. Key takeaways include:

  • Using empty to check if a DataFrame has no rows or columns.
  • A DataFrame with no rows or no columns is considered empty.
  • This property is useful for validating data before performing operations.

The DataFrame.empty property is a simple yet effective tool to verify the presence of data in a DataFrame.


Python Libraries