By using pandas.DataFrame.empty
attribute you can check if DataFrame is empty or not. We refer DataFrame as empty when it has zero rows. This pandas DataFrame empty
attribute returns a Boolean value; value True
when DataFrame is empty and False
when it is empty.
We are often required to check whether panda DataFrame is empty for several reasons and wanted to ignore performing certain operations/actions when it is empty. In this article, I will cover few examples of how to check if DataFrame is empty or not with examples.
Key Points –
- Pandas DataFrame has an
empty
attribute that returns True if the DataFrame is empty, i.e., it has no rows or columns. - Check the
size
property of the DataFrame, which represents the total number of elements. An empty DataFrame will have a size of zero. - The
shape
attribute returns a tuple representing the dimensions of the DataFrame. If either dimension is zero, the DataFrame is considered empty. - Apply the
len()
function to the DataFrame, which returns the number of rows. An empty DataFrame will have a length of zero. - Use boolean logic with DataFrame methods: Utilize boolean logic with DataFrame methods such as
any()
orall()
along specific axes (rows or columns) to check if any elements are present, indicating non-emptiness.
Notes:
- If your DataFrame contains only
NaN
/None
values then it is not considered empty sodf.empty
attribute returnsFalse
. - Note that
len(DataFrame.index)
gives you better performance compared withlen(DataFrame)
andDataFrame.empty
attribute.
1. Quick Examples of Pandas Check If DataFrame is Empty
If you are in a hurry, below are some quick examples of pandas check if DataFrame is empty.
# Check using empty attribute
print(df_empty.empty) ==> Prints True
# Check DataFrame Empty using shape()
print(df_empty.shape[0] == 0) ==> Prints True
# Check DataFrame Empty using size()
print(df_empty.size == 0) ==> Prints True
# Using len() Function
print(len(df_empty) == 0) ==> Prints True
# Using len() & index
print(len(df_empty.index) == 0) ==> Prints True
2. Create Sample DataFrame to Check If it is Empty
In order to check if DataFrame is empty, let’s create a DataFrame with empty using pandas.DataFrame()
constructor and another DataFrame with few rows.
import pandas as pd
import numpy as np
# This creates empty DataFrame
df_empty=pd.DataFrame()
# DataFrame with few rows
df_non_empty=pd.DataFrame(['A','B','C'])
3. Check DataFrame has Rows Using empty Attribute
df_empty.empty
return boolean value True
as this DataFrame has no rows and df_non_empty.empty
return False
as it has 3 rows. In a real-time application, you would do something like below using if or similar conditional statements.
# Check DataFrame is empty
if(df_empty.empty == True):
print('Empty DataFrame')
# Do some thing when DF is empty
else:
print('Non Empty DataFrame')
# Proceed with regular operation
Yields below output.
# Output:
Empty DataFrame
4. Using DataFrame.shape() & DataFrame.size to check If it is Empty
pandas.DataFrame.shape()
method returns the number of rows and number of columns as a tuple, you can use this to check if pandas DataFrame is empty. DataFrame.shape[0]
return number of rows. If you have no rows then it gives you 0 and comparing it with 0 gives you True
.
Similarly, you can also try size()
method. size() returns a count of all cells (rows * columns).
# Check DataFrame Empty using shape()
print(df_empty.shape[0] == 0) == Prints True
# Check DataFrame Empty using size()
print(df_empty.size == 0) == Prints True
5. Check Whether DataFrame is empty Using Length & Index
len(df)
function gives a number of rows in DataFrame hence, you can use this to check whether DataFrame is empty.
# Using len() Function
print(len(df_empty) == 0) ==> Prints True
But the best way to check if DataFrame is empty is using its Index. When there are no rows present, Dataframe.index
returns an empty Series, and getting the length of it’s gets you 0.
# Using len() & index
print(len(df_empty.index) == 0) ==> Prints True
Note that len(DataFrame.index)
gives you better performance compared with len(DataFrame)
and DataFrame.empty attribute
. For details check stackoverflow.com.
6. Checking Empty DataFrame If it has All NaN or None Values
If you have a pandas DataFrame with all NaN
/None
values, checking if it is an empty return False
. In order to get this as empty first, you need to drop all None/NaN values using dropna()
method. Below I have provided examples.
import numpy as np
# DataFrame with all NaN values
df_all_nan = pd.DataFrame([None,np.nan,np.NaN])
print(df_all_nan.empty) ==> Returns False
print(df_all_nan.dropna().empty) ==> Returns True
7. Complete Example
import pandas as pd
import numpy as np
# This creates empty DataFrame
df_empty=pd.DataFrame()
# DataFrame with few rows
df_non_empty = pd.DataFrame(['A','B','C'])
# Anotehr DataFrame with all NaN values
df_all_nan = pd.DataFrame([np.nan,np.nan,np.NaN])
# Check DataFrame is Empty using empty attribute
print(df_all_nan.empty)
print(df_empty.empty)
# Check using empty attribute & if condition
if(df_empty.empty == True):
print('Empty DataFrame')
# Do some thing when DF is empty
else:
print('Non Empty DataFrame')
# Proceed with regular operation
# Check DF Empty using len() & index
print(len(df_empty)==0)
print(len(df_empty.index) ==0)
# Check using share() & size
print(df_empty.size == 0)
print(df_empty.shape[0] == 0)
# Check Empty if all values are None & NaN
print(df_all_nan.empty)
print(df_all_nan.dropna().empty)
Frequently Asked Questions on Pandas Check If DataFrame is Empty
Use the empty
attribute of the DataFrame. It returns True
if the DataFrame is empty and False
otherwise.
The size
property represents the total number of elements in the DataFrame. If the size is zero, the DataFrame is considered empty.
A DataFrame with only missing values is not considered empty. The empty
attribute and other methods focus on the presence of rows or columns, regardless of the values they contain.
Using the empty
attribute is often recommended due to its clarity and simplicity. It directly provides a boolean result indicating whether the DataFrame is empty.
You can use the shape
attribute and verify if either dimension is zero. Additionally, applying the len()
function to the DataFrame and checking if the length is zero provides another approach.
While there might be subtle performance variations, the differences are generally negligible. The empty
attribute is often preferred for its simplicity, but choosing a method depends on the specific use case and personal preference.
Conclusion
In this article, you have learned how to check whether a pandas DataFrame is empty using an empty attribute, size, share, and len methods. If your DataFrame contains only NaN
/None
values then it is not considered empty so df.empty
attribute returns False
. Note that len(DataFrame.index)
gives you better performance compared with len(DataFrame)
and DataFrame.empty
attribute.
Related Articles
- How to Check If any Value is NaN in a Pandas DataFrame
- How to Add an Empty Column to a Pandas DataFrame
- Add New Column to Existing Pandas DataFrame
- Select Multiple Columns in Pandas DataFrame
- Pandas GroupBy Multiple Columns Explained
- Pandas Create Conditional Column in DataFrame
- Pandas Normalize Columns of DataFrame
- Pandas Merge Multiple DataFrames
- Pandas Get First Row Value of a Given Column
- How to Create Pandas Pivot Multiple Columns
- How to Union Pandas DataFrames using Concat?
- Replace NaN Values with Zeroes in a Column of a Pandas DataFrame