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.
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
#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.
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)
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
- Replace NaN Values with Zeroes in a Column of a Pandas DataFrame
- Add New Column to Existing Pandas DataFrame
- Select Multiple Columns in Pandas DataFrame
- Pandas GroupBy Multiple Columns Explained
- Pandas Merge Multiple DataFrames
- How to Create Pandas Pivot Multiple Columns