Bird
Raised Fist0
Pandasdata~20 mins

Combining multiple cleaning steps in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Combining Multiple Cleaning Steps
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of combined cleaning steps on a DataFrame

What is the output of the following code after applying multiple cleaning steps?

Pandas
import pandas as pd

data = {'Name': ['Alice', 'Bob', None, 'David'],
        'Age': [25, None, 30, 22],
        'City': ['New York', 'Los Angeles', 'New York', None]}
df = pd.DataFrame(data)

# Cleaning steps
result = (df.dropna(subset=['Name'])
            .fillna({'Age': df['Age'].mean(), 'City': 'Unknown'})
            .reset_index(drop=True))
print(result)
A
    Name   Age         City
0  Alice  25.0     New York
1    Bob  27.0       Los Angeles
2  David  22.0      Unknown
B
    Name   Age         City
0  Alice  25.0     New York
1    Bob  25.666667  Los Angeles
2  David  22.0      New York
C
    Name   Age         City
0  Alice  25.0     New York
1    Bob  NaN       Los Angeles
2  David  22.0      Unknown
D
    Name   Age         City
0  Alice  25.0     New York
1    Bob  25.666667  Los Angeles
2  David  22.0      Unknown
Attempts:
2 left
💡 Hint

Look carefully at how missing values are handled and what the mean age is.

❓ data_output
intermediate
1:30remaining
Number of rows after multiple cleaning steps

After applying the cleaning steps below, how many rows remain in the DataFrame?

Pandas
import pandas as pd

data = {'Product': ['A', 'B', 'C', 'D', None],
        'Price': [10, None, 15, 20, 25],
        'Stock': [100, 200, None, 150, 300]}
df = pd.DataFrame(data)

cleaned = (df.dropna(subset=['Product'])
             .fillna({'Price': 0, 'Stock': 0})
             .query('Price > 0 and Stock > 0'))
print(len(cleaned))
A3
B2
C4
D5
Attempts:
2 left
💡 Hint

Check which rows are dropped and which remain after filtering.

🔧 Debug
advanced
1:30remaining
Identify the error in combined cleaning steps

What error does the following code raise?

Pandas
import pandas as pd

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

result = df.bfill().dropna(subset=['C'])
AKeyError: 'C'
BTypeError: fillna() got an unexpected keyword argument 'method'
CAttributeError: 'DataFrame' object has no attribute 'backfill'
DNo error, returns DataFrame
Attempts:
2 left
💡 Hint

Check if the column 'C' exists before dropping rows based on it.

❓ visualization
advanced
2:00remaining
Resulting plot after cleaning and grouping

After cleaning and grouping the data, what does the bar plot show?

Pandas
import pandas as pd
import matplotlib.pyplot as plt

data = {'Category': ['X', 'Y', 'X', 'Z', None],
        'Value': [10, 20, None, 15, 5]}
df = pd.DataFrame(data)

cleaned = df.dropna(subset=['Category']).fillna({'Value': 0})
grouped = cleaned.groupby('Category').sum()

ax = grouped.plot(kind='bar', legend=False)
plt.ylabel('Sum of Values')
plt.title('Sum of Values by Category')
plt.close()  # Prevent actual plot display in test

print(grouped)
A{'Value': {'X': 10.0, 'Y': 20.0, 'Z': 15.0}}
B{'Value': {'X': 10.0, 'Y': 20.0, 'Z': 0.0}}
C{'Value': {'X': 0.0, 'Y': 20.0, 'Z': 15.0}}
D{'Value': {'X': 10.0, 'Y': 0.0, 'Z': 15.0}}
Attempts:
2 left
💡 Hint

Look at how missing values in 'Value' are replaced and how grouping sums values.

🚀 Application
expert
2:30remaining
Combining multiple cleaning steps for a real dataset

You have a DataFrame with columns 'ID', 'Score', and 'Grade'. You want to:

  • Remove rows where 'ID' is missing.
  • Fill missing 'Score' with the median score.
  • Replace missing 'Grade' with 'Incomplete'.
  • Drop rows where 'Score' is less than 50 after filling.

Which code snippet correctly performs all these steps?

Adf.fillna({'Score': df['Score'].median(), 'Grade': 'Incomplete'}).dropna(subset=['ID']).query('Score >= 50')
Bdf.query('Score >= 50').dropna(subset=['ID']).fillna({'Score': df['Score'].median(), 'Grade': 'Incomplete'})
Cdf.dropna(subset=['ID']).fillna({'Score': df['Score'].median(), 'Grade': 'Incomplete'}).query('Score >= 50')
Ddf.dropna(subset=['ID']).query('Score >= 50').fillna({'Score': df['Score'].median(), 'Grade': 'Incomplete'})
Attempts:
2 left
💡 Hint

Think about the order of operations: drop missing IDs first, then fill missing values, then filter by score.

Practice

(1/5)
1. What is the main benefit of combining multiple cleaning steps in pandas using method chaining?
easy
A. It creates multiple copies of the data.
B. It slows down the data processing.
C. It makes the code shorter and easier to read.
D. It hides errors in the data.

Solution

  1. Step 1: Understand method chaining

    Method chaining lets you apply many cleaning steps one after another in one line or block.
  2. Step 2: Benefits of chaining

    This approach reduces code length and improves readability by avoiding intermediate variables.
  3. Final Answer:

    It makes the code shorter and easier to read. -> Option C
  4. Quick Check:

    Method chaining = shorter, readable code [OK]
Hint: Chaining keeps code neat and easy to follow [OK]
Common Mistakes:
  • Thinking chaining slows down processing
  • Believing it creates multiple data copies
  • Assuming it hides data errors
2. Which of the following is the correct syntax to chain pandas cleaning methods to drop missing values and then reset the index?
easy
A. df.dropna().reset_index(drop=True
B. df.dropna.reset_index(drop=True)
C. df.dropna().reset_index(drop=True))
D. df.dropna().reset_index(drop=True)

Solution

  1. Step 1: Check method calls

    Each method must be called with parentheses: dropna() and reset_index().
  2. Step 2: Verify parentheses balance

    df.dropna().reset_index(drop=True) has balanced parentheses and correct chaining syntax.
  3. Final Answer:

    df.dropna().reset_index(drop=True) -> Option D
  4. Quick Check:

    Method calls need parentheses [OK]
Hint: Always use () after pandas methods when chaining [OK]
Common Mistakes:
  • Missing parentheses after methods
  • Unbalanced parentheses causing syntax errors
  • Using incorrect method names
3. Given the DataFrame df below, what will be the output after applying the chained cleaning steps?
import pandas as pd
df = pd.DataFrame({
  'A': [1, 2, None, 4],
  'B': ['x', None, 'y', 'z']
})
cleaned = df.dropna().reset_index(drop=True)
medium
A. {'A': [1.0, 4.0], 'B': ['x', 'y']}
B. {'A': [1.0, 4.0], 'B': ['x', 'z']}
C. {'A': [1.0, 2.0, 4.0], 'B': ['x', 'y', 'z']}
D. {'A': [1.0, 2.0, 4.0], 'B': ['x', None, 'z']}

Solution

  1. Step 1: Apply dropna()

    Rows with any missing values are removed. Row 1 and 2 have missing values, so only rows 0 and 3 remain.
  2. Step 2: Reset index

    Resetting index with drop=True gives new index 0 and 1 for the remaining rows.
  3. Final Answer:

    {'A': [1.0, 4.0], 'B': ['x', 'z']} -> Option B
  4. Quick Check:

    dropna removes rows with NaN, reset_index fixes index [OK]
Hint: dropna removes rows with NaN, reset_index fixes numbering [OK]
Common Mistakes:
  • Forgetting dropna removes rows with any NaN
  • Not resetting index after dropping rows
  • Assuming missing values are filled automatically
4. The following code is intended to clean a DataFrame by filling missing values with 0 and then dropping duplicates. What is the error?
cleaned = df.fillna(0).drop_duplicates
medium
A. Missing parentheses after drop_duplicates method.
B. fillna should be called after drop_duplicates.
C. drop_duplicates does not exist in pandas.
D. fillna cannot be chained with drop_duplicates.

Solution

  1. Step 1: Check method calls

    fillna(0) is called correctly with parentheses, but drop_duplicates is missing parentheses.
  2. Step 2: Understand method chaining syntax

    All methods must be called with parentheses to execute them.
  3. Final Answer:

    Missing parentheses after drop_duplicates method. -> Option A
  4. Quick Check:

    Methods need () to run [OK]
Hint: Always add () after pandas methods when chaining [OK]
Common Mistakes:
  • Forgetting parentheses after methods
  • Changing method order unnecessarily
  • Thinking drop_duplicates is not a pandas method
5. You have a DataFrame df with columns 'Name', 'Age', and 'Score'. Some 'Age' values are missing, and some rows are duplicates. You want to fill missing 'Age' with the average age, remove duplicates, and reset the index. Which chained code correctly does this?
hard
A. df.fillna({'Age': df['Age'].mean()}).drop_duplicates().reset_index(drop=True)
B. df.drop_duplicates().fillna(df['Age'].mean()).reset_index(drop=True)
C. df.fillna(df['Age'].mean()).drop_duplicates.reset_index(drop=True)
D. df.reset_index(drop=True).fillna({'Age': df['Age'].mean()}).drop_duplicates()

Solution

  1. Step 1: Fill missing 'Age' with average

    Use fillna with a dictionary to fill only 'Age' column with its mean.
  2. Step 2: Remove duplicates and reset index

    Then drop duplicates and reset index with drop=True to clean the DataFrame fully.
  3. Final Answer:

    df.fillna({'Age': df['Age'].mean()}).drop_duplicates().reset_index(drop=True) -> Option A
  4. Quick Check:

    Fill missing, drop duplicates, reset index in order [OK]
Hint: Fill missing with dict, then drop duplicates, reset index last [OK]
Common Mistakes:
  • Filling all columns instead of only 'Age'
  • Missing parentheses on drop_duplicates
  • Resetting index before cleaning data
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