Bird
Raised Fist0
Pandasdata~10 mins

Counting missing values in Pandas - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Counting missing values
Start with DataFrame
↓
Check each cell for missing?
↓
Count True (missing) per column
↓
Return count per column
↓
Optionally count total missing
↓
End
We start with a DataFrame, check each cell for missing values, count how many missing per column, and optionally get total missing count.
Execution Sample
Pandas
import pandas as pd

df = pd.DataFrame({
  'A': [1, None, 3],
  'B': [None, None, 6]
})
missing_count = df.isna().sum()
This code creates a DataFrame with missing values and counts missing values per column.
Execution Table
StepActionDataFrame Stateisna() Resultsum() Result
1Create DataFrame{'A':[1, None, 3], 'B':[None, None, 6]}N/AN/A
2Check missing with isna(){'A':[1, None, 3], 'B':[None, None, 6]}{'A':[False, True, False], 'B':[True, True, False]}N/A
3Sum missing per column{'A':[1, None, 3], 'B':[None, None, 6]}{'A':[False, True, False], 'B':[True, True, False]}{'A':1, 'B':2}
4EndSame as step 1Same as step 2Final missing counts per column
💡 All rows processed, missing values counted per column.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
dfNone{'A':[1, None, 3], 'B':[None, None, 6]}{'A':[1, None, 3], 'B':[None, None, 6]}{'A':[1, None, 3], 'B':[None, None, 6]}{'A':[1, None, 3], 'B':[None, None, 6]}
missing_maskNoneNone{'A':[False, True, False], 'B':[True, True, False]}{'A':[False, True, False], 'B':[True, True, False]}{'A':[False, True, False], 'B':[True, True, False]}
missing_countNoneNoneNone{'A':1, 'B':2}{'A':1, 'B':2}
Key Moments - 3 Insights
Why does df.isna() return a DataFrame of True/False instead of numbers?
df.isna() checks each cell and returns True if missing, False otherwise, as shown in execution_table step 2. This boolean mask helps count missing values.
How does sum() count missing values when applied to booleans?
In Python, True counts as 1 and False as 0. So sum() adds up True values per column, giving the count of missing values (execution_table step 3).
Can we count missing values for the whole DataFrame at once?
Yes, by adding .sum().sum() after isna(), you get total missing values in the entire DataFrame, but here we count per column first (not shown in this trace).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the missing count for column 'B'?
A1
B2
C3
D0
💡 Hint
Check the sum() Result column in execution_table row with Step 3.
According to variable_tracker, what is the value of missing_mask after step 2?
A{'A':[False, True, False], 'B':[True, True, False]}
B{'A':[True, True, False], 'B':[False, True, False]}
C{'A':[False, False, False], 'B':[True, True, True]}
DNone
💡 Hint
Look at the missing_mask row under After Step 2 column in variable_tracker.
If the DataFrame had no missing values, what would be the sum() result?
A{'A':3, 'B':3}
B{'A':1, 'B':1}
C{'A':0, 'B':0}
DNone
💡 Hint
Recall that sum() counts True values; no missing means all False, so sum is zero.
Concept Snapshot
Counting missing values in pandas:
- Use df.isna() to get True/False mask of missing cells.
- Use .sum() on mask to count missing per column.
- True counts as 1, False as 0 in sum.
- Use .sum().sum() for total missing in DataFrame.
- Useful to find data quality issues quickly.
Full Transcript
We start with a DataFrame containing some missing values. Using df.isna(), we create a mask showing True where values are missing and False otherwise. Then, summing this mask per column counts how many missing values each column has. This works because True counts as 1 and False as 0 in sums. The final result is a count of missing values per column. This method helps quickly identify where data is incomplete.

Practice

(1/5)
1. What does the pandas method df.isna().sum() do when applied to a DataFrame df?
easy
A. Counts the total number of rows in the DataFrame
B. Counts the number of missing values in each column of the DataFrame
C. Replaces missing values with zero in the DataFrame
D. Returns the data types of each column in the DataFrame

Solution

  1. Step 1: Understand df.isna()

    This method returns a DataFrame of the same shape as df with True where values are missing and False otherwise.
  2. Step 2: Apply .sum() on the boolean DataFrame

    Summing True values (which count as 1) per column gives the count of missing values in each column.
  3. Final Answer:

    Counts the number of missing values in each column of the DataFrame -> Option B
  4. Quick Check:

    df.isna().sum() counts missing per column [OK]
Hint: Use isna() then sum() to count missing per column [OK]
Common Mistakes:
  • Thinking sum() counts rows instead of missing values
  • Confusing isna() with fillna()
  • Assuming it replaces missing values
2. Which of the following is the correct syntax to count all missing values in a pandas DataFrame df?
easy
A. df.isna().count()
B. df.count().sum()
C. df.isna().sum().sum()
D. df.isnull().count()

Solution

  1. Step 1: Use df.isna() or df.isnull() to find missing values

    Both return a boolean DataFrame marking missing values as True.
  2. Step 2: Use .sum().sum() to count all missing values

    The first sum() sums missing per column, the second sums those counts to get total missing in the DataFrame.
  3. Final Answer:

    df.isna().sum().sum() -> Option C
  4. Quick Check:

    Total missing = df.isna().sum().sum() [OK]
Hint: Double sum() after isna() counts all missing values [OK]
Common Mistakes:
  • Using count() which counts non-missing values
  • Using isna().count() which counts all rows, not missing
  • Confusing isna() with isnull().count()
3. Given the DataFrame df below:
import pandas as pd
data = {'A': [1, None, 3], 'B': [None, None, 6], 'C': [7, 8, 9]}
df = pd.DataFrame(data)

What is the output of df.isna().sum()?
medium
A. A 1 B 1 C 1 dtype: int64
B. A 0 B 0 C 0 dtype: int64
C. A 2 B 1 C 0 dtype: int64
D. A 1 B 2 C 0 dtype: int64

Solution

  1. Step 1: Identify missing values in each column

    Column A has one None (missing), column B has two None values, column C has none.
  2. Step 2: Apply df.isna().sum()

    This counts missing per column: A=1, B=2, C=0.
  3. Final Answer:

    A 1 B 2 C 0 dtype: int64 -> Option D
  4. Quick Check:

    Count missing per column matches output [OK]
Hint: Count None or NaN per column to verify sums [OK]
Common Mistakes:
  • Mixing counts of missing and non-missing
  • Miscounting missing in column B
  • Assuming None counts as zero
4. You run the code below but get an error:
import pandas as pd
data = {'X': [1, 2, None], 'Y': [None, 5, 6]}
df = pd.DataFrame(data)
missing = df.isna.sum()

What is the error and how to fix it?
medium
A. Missing parentheses after isna; fix with df.isna().sum()
B. DataFrame missing columns; add columns before counting
C. sum() is not a pandas method; use count() instead
D. None values cause error; replace with zeros first

Solution

  1. Step 1: Identify the error in method call

    The code uses df.isna.sum() missing parentheses after isna, which is a method.
  2. Step 2: Correct the syntax

    Use df.isna().sum() to call the method and then sum missing values.
  3. Final Answer:

    Missing parentheses after isna; fix with df.isna().sum() -> Option A
  4. Quick Check:

    Method calls need parentheses [OK]
Hint: Remember to add () after isna to call the method [OK]
Common Mistakes:
  • Forgetting parentheses on isna()
  • Using sum() instead of count() incorrectly
  • Thinking None causes errors in counting
5. You have a DataFrame df with columns ['age', 'income', 'score']. You want to find the percentage of missing values in each column. Which code snippet correctly calculates this?
hard
A. df.isna().sum() / len(df) * 100
B. df.isna().count() / len(df) * 100
C. df.count() / df.isna().sum() * 100
D. df.isna().sum() * 100

Solution

  1. Step 1: Count missing values per column

    Use df.isna().sum() to get missing counts per column.
  2. Step 2: Calculate percentage of missing values

    Divide missing counts by total rows len(df) and multiply by 100 to get percentage.
  3. Final Answer:

    df.isna().sum() / len(df) * 100 -> Option A
  4. Quick Check:

    Missing % = (missing count / total rows) * 100 [OK]
Hint: Divide missing count by total rows, multiply by 100 [OK]
Common Mistakes:
  • Using count() which counts non-missing values
  • Multiplying missing count by 100 without dividing
  • Dividing count by missing count incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard