“Is it possible to check if a cell is empty in a Pandas DataFrame?”

“Is it possible to check if a cell is empty in a Pandas DataFrame?”

The question “Is it possible to check if a cell is empty in a Pandas DataFrame?” refers to the ability to determine if a specific cell within a Pandas DataFrame, which is a data structure used for data analysis in Python, does not contain any value. This inquiry is relevant for data manipulation and cleaning purposes, as well as for ensuring data accuracy and quality. The answer to this question can be found through various methods and functions available in Pandas, which allow for the identification and handling of empty cells in a DataFrame.

Check if Cell is Empty in Pandas DataFrame


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 emptyprint(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 emptyprint(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:

Cite this article

stats writer (2024). “Is it possible to check if a cell is empty in a Pandas DataFrame?”. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/is-it-possible-to-check-if-a-cell-is-empty-in-a-pandas-dataframe/

stats writer. "“Is it possible to check if a cell is empty in a Pandas DataFrame?”." PSYCHOLOGICAL SCALES, 30 Jun. 2024, https://scales.arabpsychology.com/stats/is-it-possible-to-check-if-a-cell-is-empty-in-a-pandas-dataframe/.

stats writer. "“Is it possible to check if a cell is empty in a Pandas DataFrame?”." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/is-it-possible-to-check-if-a-cell-is-empty-in-a-pandas-dataframe/.

stats writer (2024) '“Is it possible to check if a cell is empty in a Pandas DataFrame?”', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/is-it-possible-to-check-if-a-cell-is-empty-in-a-pandas-dataframe/.

[1] stats writer, "“Is it possible to check if a cell is empty in a Pandas DataFrame?”," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. “Is it possible to check if a cell is empty in a Pandas DataFrame?”. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top