Bird
Raised Fist0
Pandasdata~20 mins

Handling inconsistent values in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Handling Inconsistent Values
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of filling missing values with mode
What is the output DataFrame after filling missing values in the 'color' column with the mode of that column?
Pandas
import pandas as pd

df = pd.DataFrame({'color': ['red', 'blue', None, 'blue', None, 'red']})
mode_color = df['color'].mode()[0]
df['color'] = df['color'].fillna(mode_color)
print(df)
A
   color
0    red
1   blue
2   blue
3   blue
4   blue
5    red
B
   color
0    red
1   blue
2    red
3   blue
4    red
5    red
C
   color
0    red
1   blue
2  None
3   blue
4  None
5    red
D
   color
0    red
1   blue
2   blue
3   blue
4  None
5    red
Attempts:
2 left
💡 Hint
The mode is the most frequent value in the column. Filling missing values replaces NaNs with this value.
❓ data_output
intermediate
1:30remaining
Count unique values after standardizing text
After converting all entries in the 'fruit' column to lowercase, how many unique values remain?
Pandas
import pandas as pd

df = pd.DataFrame({'fruit': ['Apple', 'apple', 'Banana', 'BANANA', 'banana', 'Cherry']})
df['fruit'] = df['fruit'].str.lower()
unique_count = df['fruit'].nunique()
print(unique_count)
A4
B6
C3
D5
Attempts:
2 left
💡 Hint
Lowercasing makes 'Apple' and 'apple' the same value.
🔧 Debug
advanced
2:00remaining
Identify error in replacing inconsistent categorical values
What error does this code raise when trying to replace inconsistent values in the 'status' column?
Pandas
import pandas as pd

df = pd.DataFrame({'status': ['Active', 'active', 'Inactive', 'inactive', 'Pending']})
df['status'] = df['status'].replace({'active': 'Active', 'inactive': 'Inactive'})
print(df)
AValueError: cannot replace with different length
BKeyError: 'active' not found in axis
CTypeError: unhashable type: 'dict'
D
No error; output is:
  status
0  Active
1  Active
2  Inactive
3  Inactive
4  Pending
Attempts:
2 left
💡 Hint
Check if replace is case sensitive and if keys exist in the column.
❓ visualization
advanced
2:30remaining
Visualizing inconsistent data before and after cleaning
Which plot correctly shows the count of each category in the 'department' column before and after fixing inconsistent capitalization?
Pandas
import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'department': ['Sales', 'sales', 'HR', 'hr', 'IT', 'it', 'IT']})
df['department_clean'] = df['department'].str.upper()
counts_before = df['department'].value_counts()
counts_after = df['department_clean'].value_counts()

plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
counts_before.plot(kind='bar', title='Before Cleaning')
plt.subplot(1,2,2)
counts_after.plot(kind='bar', title='After Cleaning')
plt.tight_layout()
plt.show()
ALeft plot shows 2 bars for 'Sales' and 'sales'; right plot shows 1 bar for 'SALES'.
BLeft plot shows 1 bar for 'Sales'; right plot shows 2 bars for 'SALES' and 'IT'.
CLeft plot shows 3 bars for 'Sales', 'HR', 'IT'; right plot shows 3 bars for 'SALES', 'HR', 'IT'.
DLeft plot shows 6 bars for each entry; right plot shows 1 bar for all combined.
Attempts:
2 left
💡 Hint
Check how value_counts counts exact matches before and after uppercasing.
🚀 Application
expert
3:00remaining
Handling mixed data types in a column for analysis
Given a DataFrame column 'age' with mixed types (integers, strings, and NaNs), which option correctly converts all valid ages to integers and replaces invalid entries with the median age?
Pandas
import pandas as pd
import numpy as np

df = pd.DataFrame({'age': [25, '30', 'unknown', np.nan, 22, '27']})

# Your task: convert 'age' to integers, replace invalid with median

# median calculation should ignore invalid and NaN values

# Which option achieves this correctly?
A
df['age'] = pd.to_numeric(df['age'], errors='coerce')
median_age = int(df['age'].median())
df['age'] = df['age'].fillna(median_age).astype(int)
print(df)
B
df['age'] = df['age'].astype(int)
median_age = df['age'].median()
df['age'] = df['age'].fillna(median_age)
print(df)
C
df['age'] = df['age'].replace('unknown', 0).astype(int)
median_age = df['age'].median()
df['age'] = df['age'].fillna(median_age)
print(df)
D
df['age'] = df['age'].apply(lambda x: int(x) if x.isdigit() else np.nan)
median_age = df['age'].median()
df['age'] = df['age'].fillna(median_age).astype(int)
print(df)
Attempts:
2 left
💡 Hint
Use pd.to_numeric with errors='coerce' to convert and set invalid to NaN.

Practice

(1/5)
1. What is the main purpose of handling inconsistent values in a pandas DataFrame?
easy
A. To clean data so analysis results are accurate and reliable
B. To make the DataFrame larger by adding more rows
C. To convert all numbers to strings
D. To sort the DataFrame by index

Solution

  1. Step 1: Understand inconsistent values

    Inconsistent values are errors or variations in data that can cause wrong analysis results.
  2. Step 2: Purpose of handling them

    Cleaning these values ensures the data is uniform and trustworthy for analysis.
  3. Final Answer:

    To clean data so analysis results are accurate and reliable -> Option A
  4. Quick Check:

    Cleaning data = Accurate analysis [OK]
Hint: Cleaning inconsistent data ensures correct analysis results [OK]
Common Mistakes:
  • Thinking inconsistent values add more data
  • Confusing cleaning with sorting
  • Assuming conversion to string fixes inconsistencies
2. Which pandas method is commonly used to replace inconsistent values in a DataFrame column?
easy
A. df.sort_values()
B. df.dropna()
C. df.replace()
D. df.groupby()

Solution

  1. Step 1: Identify method for replacing values

    df.replace() is designed to substitute specified values with new ones.
  2. Step 2: Confirm other methods' purposes

    df.dropna() removes missing data, df.sort_values() sorts data, df.groupby() groups data; none replace values.
  3. Final Answer:

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

    Replace values = df.replace() [OK]
Hint: Use df.replace() to swap inconsistent values [OK]
Common Mistakes:
  • Using dropna() to replace values
  • Confusing sorting with replacing
  • Trying to group data to fix values
3. Given the DataFrame:
import pandas as pd
df = pd.DataFrame({'color': ['red', 'Red', 'blue', 'Blue', 'RED']})
df['color'] = df['color'].str.lower()

What will be the unique values in df['color'] after this code runs?
medium
A. ['red', 'blue']
B. ['Red', 'Blue', 'RED']
C. ['red', 'Red', 'blue', 'Blue', 'RED']
D. ['RED', 'BLUE']

Solution

  1. Step 1: Understand str.lower() effect

    str.lower() converts all strings in the column to lowercase.
  2. Step 2: Apply to values and find unique

    All 'red', 'Red', 'RED' become 'red'; 'blue', 'Blue' become 'blue'. Unique values are ['red', 'blue'].
  3. Final Answer:

    ['red', 'blue'] -> Option A
  4. Quick Check:

    Lowercase unique values = ['red', 'blue'] [OK]
Hint: str.lower() unifies case to find unique values [OK]
Common Mistakes:
  • Thinking original case stays
  • Assuming unique keeps all original variants
  • Confusing str.lower() with str.upper()
4. What is wrong with this code snippet for fixing inconsistent 'gender' values?
df['gender'].replace({'M': 'Male', 'F': 'Female'})

Why might it not update the DataFrame as expected?
medium
A. Because replace() cannot handle dictionaries
B. Because replace() needs inplace=true or assignment to update df
C. Because 'M' and 'F' are not valid keys
D. Because replace() only works on numeric columns

Solution

  1. Step 1: Check replace() behavior

    By default, replace() returns a new Series and does not change df unless inplace=true or assigned.
  2. Step 2: Identify missing update step

    The code does not assign back or use inplace=true, so df['gender'] remains unchanged.
  3. Final Answer:

    Because replace() needs inplace=true or assignment to update df -> Option B
  4. Quick Check:

    replace() without assignment = no change [OK]
Hint: Always assign or use inplace=true with replace() [OK]
Common Mistakes:
  • Assuming replace() changes df without assignment
  • Thinking replace() can't use dict
  • Believing replace() only works on numbers
5. You have a DataFrame with a 'status' column containing inconsistent values like 'Active', 'active', 'ACTIVE', and some misspelled as 'Actve'. Which sequence of pandas operations best cleans this column to have only 'Active' or 'Inactive' values?
hard
A. df['status'] = df['status'].str.strip().replace({'Actve': 'Inactive'})"
B. df['status'] = df['status'].replace({'Active': 'active', 'ACTIVE': 'active'}).str.upper()"
C. df['status'] = df['status'].fillna('Inactive').replace({'active': 'Active'})"
D. df['status'] = df['status'].str.lower().replace({'actve': 'active'}).str.capitalize()"

Solution

  1. Step 1: Normalize case to lowercase

    Using str.lower() converts all variations like 'Active', 'ACTIVE' to 'active'.
  2. Step 2: Fix misspelled value

    replace({'actve': 'active'}) corrects the misspelled 'actve' to 'active'.
  3. Step 3: Capitalize for consistent formatting

    str.capitalize() changes 'active' to 'Active' for uniform appearance.
  4. Final Answer:

    df['status'] = df['status'].str.lower().replace({'actve': 'active'}).str.capitalize() -> Option D
  5. Quick Check:

    Lowercase + replace typo + capitalize = clean status [OK]
Hint: Lowercase, fix typos, then capitalize for consistent values [OK]
Common Mistakes:
  • Not fixing misspelled values
  • Skipping case normalization
  • Replacing with wrong values
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