📖 Scenario: Imagine you work in a health clinic. You have a small table of patient data with their ages and blood pressure readings. Some readings are missing. You want to understand why missing data can cause problems before fixing it.
🎯 Goal: You will create a small patient data table with missing values, count how many values are missing, and see how missing data affects calculations like average blood pressure.
📋 What You'll Learn
Create a pandas DataFrame with patient names, ages, and blood pressure readings including missing values
Create a variable to count missing blood pressure values
Calculate the average blood pressure without handling missing data
Print the count of missing values and the average blood pressure
💡 Why This Matters
🌍 Real World
In real health data, missing measurements happen often. Knowing how to find and handle them helps keep analysis accurate.
💼 Career
Data scientists must detect and manage missing data to build reliable models and reports.
Progress0 / 4 steps
1
Create patient data with missing values
Create a pandas DataFrame called patients with these exact columns and values: 'Name': ['Alice', 'Bob', 'Charlie', 'David', 'Eva'] 'Age': [25, 30, 35, 40, 45] 'BloodPressure': [120, None, 130, None, 110] Use import pandas as pd before creating the DataFrame.
Pandas
Hint
Use pd.DataFrame with a dictionary of lists. Use None for missing values.
2
Count missing blood pressure values
Create a variable called missing_count that counts how many missing values are in the 'BloodPressure' column of patients. Use the isna() method and sum().
Pandas
Hint
Use patients['BloodPressure'].isna() to get True for missing values, then sum them.
3
Calculate average blood pressure without handling missing data
Create a variable called average_bp that calculates the mean of the 'BloodPressure' column in patients without removing missing values explicitly. Use the mean() method directly.
Pandas
Hint
Use patients['BloodPressure'].mean() directly. It ignores missing values automatically.
4
Print missing count and average blood pressure
Print the text Missing blood pressure values: followed by the value of missing_count. Then print Average blood pressure: followed by the value of average_bp. Use two separate print() statements.
Pandas
Hint
Use print("Missing blood pressure values:", missing_count) and print("Average blood pressure:", average_bp).
Practice
(1/5)
1. Why is it important to handle missing data in a pandas DataFrame?
easy
A. Because missing data can cause errors or lead to wrong conclusions.
B. Because pandas does not allow any missing data by default.
C. Because missing data always improves model accuracy.
D. Because missing data automatically fills with zeros.
Solution
Step 1: Understand the impact of missing data
Missing data can cause errors in calculations or models and lead to incorrect results.
Step 2: Recognize the need for handling missing data
Handling missing data ensures the analysis is accurate and reliable.
Final Answer:
Because missing data can cause errors or lead to wrong conclusions. -> Option A
Quick Check:
Missing data impact = errors/wrong conclusions [OK]
Hint: Missing data can break analysis; always check first [OK]
Common Mistakes:
Assuming pandas never allows missing data
Thinking missing data improves accuracy
Believing missing data fills automatically
2. Which pandas function is used to find missing values in a DataFrame?
easy
A. replace()
B. fillna()
C. dropna()
D. isnull()
Solution
Step 1: Identify function to detect missing data
The isnull() function returns True for missing values in a DataFrame.
Step 2: Differentiate from other functions
fillna() fills missing data, dropna() removes rows/columns with missing data, and replace() replaces specified values.
Final Answer:
isnull() -> Option D
Quick Check:
Find missing = isnull() [OK]
Hint: Use isnull() to spot missing data quickly [OK]
A. fillna() returns a new DataFrame; df is unchanged without assignment.
B. df.mean() returns a Series, which cannot be used in fillna().
C. fillna() modifies df in place by default, no error here.
D. None values cannot be replaced using fillna().
Solution
Step 1: Understand fillna() behavior
fillna() returns a new DataFrame by default and does not change df unless inplace=True or reassigned.
Step 2: Check code effect
Since df.fillna(df.mean()) is not assigned back, df remains unchanged, so missing values are not filled.
Final Answer:
fillna() returns a new DataFrame; df is unchanged without assignment. -> Option A
Quick Check:
fillna() needs assignment or inplace=True [OK]
Hint: Assign fillna() result or use inplace=True to update df [OK]
Common Mistakes:
Assuming fillna() changes df without assignment
Thinking df.mean() is invalid input
Believing None cannot be replaced
5. You have a DataFrame with many missing values scattered across columns. Which approach best preserves data while handling missing values for analysis?
hard
A. Use dropna() to remove all rows with any missing data.
B. Use fillna() with column means to replace missing values.
C. Ignore missing data and proceed with analysis.
D. Replace missing values with zeros regardless of context.
Solution
Step 1: Evaluate dropna() impact
Dropping all rows with missing data can remove too much data, losing valuable information.
Step 2: Consider fillna() with column means
Replacing missing values with column means keeps data size and reduces bias from missingness.
Step 3: Assess ignoring or zero replacement
Ignoring missing data risks errors; zero replacement may distort data if zeros are not meaningful.
Final Answer:
Use fillna() with column means to replace missing values. -> Option B
Quick Check:
fillna() with means preserves data best [OK]
Hint: Fill missing with means to keep data and reduce bias [OK]
Common Mistakes:
Dropping too many rows with dropna()
Ignoring missing data effects
Replacing missing with zeros blindly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.