Missing data strategies decision in Pandas - Mini Project: Build & Apply
df with these exact columns and values:'CustomerID': [101, 102, 103, 104, 105],'Age': [25, None, 22, None, 28],'City': ['New York', 'Los Angeles', None, 'Chicago', 'Houston'],'PurchaseAmount': [250.0, 300.0, None, 150.0, None].Use pd.DataFrame with a dictionary where keys are column names and values are lists including None for missing data.
max_missing and set it to 2. This will be the maximum number of missing values allowed per column.Just create a variable max_missing and assign the number 2.
cleaned_df by dropping columns from df that have more than max_missing missing values. Then fill the remaining missing values in cleaned_df with 0.Use dropna with axis=1 and thresh=len(df) - max_missing to drop columns. Then use fillna(0) to fill missing values.
cleaned_df DataFrame to see the result after handling missing data.Use print(cleaned_df) to show the cleaned data.
Practice
What is the main purpose of using dropna() in pandas?
Solution
Step 1: Understand
dropna()functiondropna()removes rows or columns that have missing values (NaN).Step 2: Compare with other options
Replacing missing data or converting data types are done by other functions likefillna()orastype().Final Answer:
To remove rows or columns that contain missing data -> Option AQuick Check:
dropna()removes missing data rows/columns [OK]
- Confusing dropna with fillna
- Thinking dropna fills missing values
- Assuming dropna changes data types
Which of the following is the correct syntax to fill missing values in a DataFrame df with the value 0?
?
Solution
Step 1: Recall fillna syntax
The correct method to fill missing values isfillna()with the value as argument.Step 2: Check each option
df.fillna(0) usesdf.fillna(0)which is correct. Others have wrong method names or parameters.Final Answer:
df.fillna(0) -> Option AQuick Check:
Usefillna(value)to fill missing values [OK]
- Using dropna instead of fillna
- Wrong method name like fill() or replace()
- Passing parameters incorrectly
Given the DataFrame df below, what will be the output of df.fillna(method='ffill')?
import pandas as pd
df = pd.DataFrame({'A': [1, None, 3, None], 'B': [None, 2, None, 4]})
print(df.fillna(method='ffill'))Solution
Step 1: Understand forward fill method
method='ffill'fills missing values by copying the last known non-missing value forward.Step 2: Apply to DataFrame
For column A: 1 stays, None becomes 1, 3 stays, None becomes 3. For B: None stays None (no previous), 2 stays, None becomes 2, 4 stays.Final Answer:
Fills missing values by propagating last valid observation forward -> Option CQuick Check:
ffill copies last valid value forward [OK]
- Thinking fillna(method='ffill') fills with zero
- Confusing ffill with dropping rows
- Expecting error from method parameter
What is wrong with the following code if the goal is to remove all rows with missing data from DataFrame df?
df.fillna(drop=True)
Solution
Step 1: Check method and parameters
fillna()is for filling missing values and does not accept adropparameter.Step 2: Correct method for removing rows
To remove rows with missing data, usedropna()instead.Final Answer:
fillna()does not have adropparameter; usedropna()instead -> Option BQuick Check:
Use dropna() to remove missing data rows [OK]
- Using fillna() with invalid parameters
- Confusing fillna and dropna functions
- Expecting fillna to remove rows
You have a DataFrame df with many missing values scattered randomly. You want to keep as much data as possible but avoid rows with more than 2 missing values. Which pandas method and parameter combination is best?
Solution
Step 1: Understand the threshold parameter
dropna(thresh=n)keeps rows with at leastnnon-missing values.Step 2: Calculate threshold for max 2 missing values
If total columns arelen(df.columns), then rows must have at leastlen(df.columns) - 2non-missing values to keep.Step 3: Evaluate other options
Filling with zero or backward fill changes data but does not drop rows. Dropping columns removes data unnecessarily.Final Answer:
Usedf.dropna(thresh=len(df.columns)-2)to drop rows with more than 2 missing values -> Option DQuick Check:
dropna(thresh) keeps rows with enough non-NaNs [OK]
- Using fillna instead of dropping rows
- Dropping columns instead of rows
- Misunderstanding thresh parameter
Ready to prove it?
Pass the test and this pattern moves unknown
