You can use df.empty to check if Pandas DataFrame is empty:
df = 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 boxes = {'Color': ['Blue','Blue','Green','Green','Red','Red'], 'Height': [15,20,25,20,15,25] } df = pd.DataFrame(boxes, columns = ['Color','Height']) 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 = df.empty
Therefore, the complete Python code is:
import pandas as pd boxes = {'Color': ['Blue','Blue','Green','Green','Red','Red'], 'Height': [15,20,25,20,15,25] } df = pd.DataFrame(boxes, columns = ['Color','Height']) df = df.empty print (df)
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 = df.drop(['Color','Height'],axis=1)
Here is the complete Python code to drop all the columns, and then check if the DataFrame is empty:
import pandas as pd boxes = {'Color': ['Blue','Blue','Green','Green','Red','Red'], 'Height': [15,20,25,20,15,25] } df = pd.DataFrame(boxes, columns = ['Color','Height']) df = df.drop(['Color','Height'],axis=1) df = df.empty print (df)
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,columns=['first_column','second_column']) 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,columns=['first_column','second_column']) df = df.empty print (df)
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 = df.dropna()
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,columns=['first_column','second_column']) df = df.dropna() df = df.empty print (df)
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.