0
0
Data-analysis-pythonHow-ToBeginner ยท 3 min read

How to Check Missing Values in Python Easily

To check missing values in Python, use pandas library's isnull() or isna() functions on your data. These functions return a boolean mask showing where values are missing (NaN). You can also use sum() to count how many missing values exist in each column.
๐Ÿ“

Syntax

Use pandas.DataFrame.isnull() or pandas.DataFrame.isna() to find missing values in a DataFrame. Both work the same way.

  • df.isnull(): Returns a DataFrame of the same shape with True where values are missing.
  • df.isna(): Alias for isnull().
  • df.isnull().sum(): Counts missing values per column.
python
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, None],
    'B': [None, 2, 3]
})

missing_mask = df.isnull()
missing_count = df.isnull().sum()
๐Ÿ’ป

Example

This example shows how to create a DataFrame with missing values and check where they are using isnull(). It also counts missing values per column.

python
import pandas as pd

# Create a DataFrame with missing values
data = {'Name': ['Alice', 'Bob', None, 'David'],
        'Age': [25, None, 30, 22],
        'City': ['NY', 'LA', 'Chicago', None]}
df = pd.DataFrame(data)

# Check missing values (True means missing)
missing = df.isnull()
print('Missing values mask:')
print(missing)

# Count missing values per column
missing_counts = df.isnull().sum()
print('\nMissing values count per column:')
print(missing_counts)
Output
Missing values mask: Name Age City 0 False False False 1 False True False 2 True False False 3 False False True Missing values count per column: Name 1 Age 1 City 1 dtype: int64
โš ๏ธ

Common Pitfalls

One common mistake is confusing None and NaN. In pandas, missing numeric values are usually NaN (from numpy), while missing object/string values can be None. Both are detected by isnull().

Another pitfall is forgetting to import pandas or not using a DataFrame or Series, which causes errors.

python
import pandas as pd

# Wrong: Using Python list without pandas
data = [1, None, 3]
# print(data.isnull())  # This will cause an error because list has no isnull()

# Right: Use pandas Series or DataFrame
series = pd.Series(data)
print(series.isnull())
Output
0 False 1 True 2 False dtype: bool
๐Ÿ“Š

Quick Reference

Here is a quick summary of useful functions to check missing values in pandas:

FunctionDescription
df.isnull()Returns True where values are missing (NaN or None)
df.isna()Alias for isnull()
df.notnull()Returns True where values are NOT missing
df.isnull().sum()Counts missing values per column
df.dropna()Removes rows with missing values
โœ…

Key Takeaways

Use pandas' isnull() or isna() to find missing values in data.
Sum the boolean mask to count missing values per column easily.
Both None and NaN are detected as missing by isnull().
Always work with pandas DataFrame or Series to use these methods.
Remember to import pandas before checking missing values.