Challenge - 5 Problems
Unique Counts Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nunique() on a DataFrame column
What is the output of the following code snippet?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 2, 3, 3, 3], 'B': ['x', 'y', 'y', 'z', 'z', 'z']}) result = df['A'].nunique() print(result)
Attempts:
2 left
💡 Hint
Count how many different numbers appear in column 'A'.
✗ Incorrect
The column 'A' has values [1, 2, 2, 3, 3, 3]. The unique values are 1, 2, and 3, so the count is 3.
❓ data_output
intermediate2:00remaining
Unique counts per column in a DataFrame
What is the output of the following code?
Pandas
import pandas as pd df = pd.DataFrame({'X': [1, 1, 2, 2], 'Y': ['a', 'b', 'a', 'b'], 'Z': [5, 5, 5, 5]}) result = df.nunique() print(result)
Attempts:
2 left
💡 Hint
Count unique values in each column separately.
✗ Incorrect
Column 'X' has unique values 1 and 2 (2 unique), 'Y' has 'a' and 'b' (2 unique), 'Z' has only 5 (1 unique).
🔧 Debug
advanced2:00remaining
Error raised by incorrect use of nunique()
What error does the following code raise?
Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3]}) result = df.nunique(axis=2) print(result)
Attempts:
2 left
💡 Hint
Check valid axis values for DataFrame methods.
✗ Incorrect
DataFrames only support axis=0 (rows) or axis=1 (columns). axis=2 is invalid and raises ValueError.
🚀 Application
advanced2:00remaining
Counting unique values after filtering
Given the DataFrame below, what is the output of the code?
Pandas
import pandas as pd df = pd.DataFrame({'Category': ['A', 'B', 'A', 'C', 'B', 'C'], 'Value': [10, 20, 10, 30, 20, 40]}) filtered = df[df['Value'] > 15] result = filtered['Category'].nunique() print(result)
Attempts:
2 left
💡 Hint
Filter rows where Value > 15, then count unique categories.
✗ Incorrect
Filtered rows have Values 20, 30, 20, 40 with Categories B, C, B, C. Unique categories are B and C, count is 2.
🧠 Conceptual
expert2:00remaining
Understanding nunique() with dropna parameter
What is the output of this code snippet?
Pandas
import pandas as pd import numpy as np df = pd.DataFrame({'A': [1, 2, 2, np.nan, np.nan]}) result = df['A'].nunique(dropna=False) print(result)
Attempts:
2 left
💡 Hint
dropna=False counts NaN as a unique value.
✗ Incorrect
The column 'A' has values [1, 2, 2, NaN, NaN]. The unique values are 1, 2, and NaN. Since dropna=False, NaN is counted as a unique value, so the count is 3.