You can use the following basic syntax to check if a specific cell is empty in a pandas DataFrame:
#check if value in first row of column 'A' is empty print(pd.isnull(df.loc[0, 'A'])) #print value in first row of column 'A' print(df.loc[0, 'A'])
The following example shows how to use this syntax in practice.
Example: Check if Cell is Empty in Pandas DataFrame
Suppose we have the following pandas DataFrame:
import pandas as pd import numpy as np #create DataFrame df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'], 'points': [18, np.nan, 19, 14, 14, 11, 20, 28], 'assists': [5, 7, 7, 9, np.nan, 9, 9, 4], 'rebounds': [11, 8, 10, 6, 6, 5, 9, np.nan]}) #view DataFrame df team points assists rebounds 0 A 18.0 5.0 11.0 1 B NaN 7.0 8.0 2 C 19.0 7.0 10.0 3 D 14.0 9.0 6.0 4 E 14.0 NaN 6.0 5 F 11.0 9.0 5.0 6 G 20.0 9.0 9.0 7 H 28.0 4.0 NaN
We can use the following code to check if the value in row index number one and column points is null:
#check if value in index row 1 of column 'points' is empty print(pd.isnull(df.loc[1, 'points'])) True
A value of True indicates that the value in row index number one of the “points” column is indeed empty.
We can also use the following code to print the actual value in row index number one of the “points” column:
#print value in index row 1 of column 'points' print(df.loc[1, 'points']) nan
The output tells us that the value in row index number one of the “points” column is nan, which is equivalent to an empty cell.
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Set Value for a Specific Cell in Pandas
How to Get Cell Value in Pandas
How to Replace NaN Values with Zero in Pandas