0
0
Pandasdata~20 mins

nunique() for unique counts in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unique Counts Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A2
B3
C6
D1
Attempts:
2 left
💡 Hint
Count how many different numbers appear in column 'A'.
data_output
intermediate
2: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)
A
X    1
Y    1
Z    1
dtype: int64
B
X    4
Y    4
Z    4
dtype: int64
C
X    2
Y    2
Z    1
dtype: int64
D
X    2
Y    1
Z    1
dtype: int64
Attempts:
2 left
💡 Hint
Count unique values in each column separately.
🔧 Debug
advanced
2: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)
AValueError: No axis named 2 for object type DataFrame
BTypeError: nunique() got an unexpected keyword argument 'axis'
CAttributeError: 'DataFrame' object has no attribute 'nunique'
DNo error, prints the unique counts
Attempts:
2 left
💡 Hint
Check valid axis values for DataFrame methods.
🚀 Application
advanced
2: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)
A1
B3
C4
D2
Attempts:
2 left
💡 Hint
Filter rows where Value > 15, then count unique categories.
🧠 Conceptual
expert
2: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)
A3
B2
C4
D5
Attempts:
2 left
💡 Hint
dropna=False counts NaN as a unique value.