How do I count the NaN values in a column in pandas DataFrame?
In Pandas, NaN (Not a Number) values often indicate missing or undefined data. To count how many NaNs exist in a particular column, you can combine isna()
(or isnull()
) and sum()
:
import pandas as pd import numpy as np df = pd.DataFrame({ "A": [1, 2, np.nan, 4], "B": [np.nan, 5, 6, np.nan] }) # Count the NaNs in column 'A' nan_count_A = df["A"].isna().sum() print("Number of NaNs in column A:", nan_count_A) # Count the NaNs in column 'B' nan_count_B = df["B"].isna().sum() print("Number of NaNs in column B:", nan_count_B)
df["A"].isna()
creates a boolean Series that marks where values in column "A" are NaN (True
) or not (False
)..sum()
on this boolean Series adds up theTrue
values (counting them as 1 each), giving the total number of NaNs.
Counting NaNs in All Columns
If you ever need to count NaNs across all columns at once:
df.isna().sum()
This returns a Series with each column name and its corresponding NaN count.
Level Up Your Python & Data Skills
To handle missing data effectively in larger projects, you’ll benefit from a strong foundation in Python. Grokking Python Fundamentals by DesignGurus.io can help reinforce essential Python concepts, coding best practices, and more advanced techniques, ensuring you’re well-equipped for data manipulation and analysis.
With these approaches, counting and handling NaN values in your DataFrame becomes a straightforward step in your data cleaning workflow!