Pandas – Check if DataFrame is Empty
To check if DataFrame is empty in Pandas, use DataFrame.empty
property.
DataFrame.empty
returns a boolean value indicating whether this DataFrame is empty or not. If the DataFrame is empty, True
is returned. If the DataFrame is not empty, False
is returned.
Example 1: Empty DataFrame
In this example, we will initialize an empty DataFrame and check if the DataFrame is empty using DataFrame.empty
property.
Python Program
import pandas as pd
#initialize a dataframe
df = pd.DataFrame()
isempty = df.empty
print('Is the DataFrame empty :', isempty)
df = pd.DataFrame()
initializes an empty dataframe. And then df.empty
checks if the dataframe is empty. Since the dataframe is empty, we would get boolean value of True
to the variable isempty
.
Output
Is the DataFrame empty : True
Example 2: Non-empty DataFrame
In the following example, we have initialized a DataFrame with some rows and then check if DataFrame.empty
returns False
.
Python Program
import pandas as pd
#initialize a dataframe
df = pd.DataFrame(
[[21, 72, 67.1],
[23, 78, 69.5],
[32, 74, 56.6],
[52, 54, 76.2]],
columns=['a', 'b', 'c'])
isempty = df.empty
print('Is the DataFrame empty :', isempty)
Output
Is the DataFrame empty : False
Summary
In this Pandas Tutorial, we learned how to check if a Pandas DataFrame is empty or not.