Challenge - 5 Problems
Cardinality 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?
Data Analysis Python
import pandas as pd data = {'fruit': ['apple', 'banana', 'apple', 'orange', 'banana', 'banana']} df = pd.DataFrame(data) result = df['fruit'].nunique() print(result)
Attempts:
2 left
💡 Hint
Count how many unique fruit names appear in the list.
✗ Incorrect
The column 'fruit' contains three unique values: 'apple', 'banana', and 'orange'. The nunique() function counts these unique values, so the output is 3.
❓ data_output
intermediate2:00remaining
Number of unique values per column
Given the DataFrame below, what is the output of df.nunique()?
Data Analysis Python
import pandas as pd data = {'A': [1, 2, 2, 3], 'B': ['x', 'y', 'x', 'z'], 'C': [True, True, False, False]} df = pd.DataFrame(data) result = df.nunique() print(result)
Attempts:
2 left
💡 Hint
Count unique values in each column separately.
✗ Incorrect
Column A has values [1,2,2,3] with 3 unique values (1,2,3). Column B has ['x','y','x','z'] with 3 unique values (x,y,z). Column C has [True,True,False,False] with 2 unique values (True, False).
🔧 Debug
advanced2:00remaining
Identify the error in using nunique()
What error will this code produce?
Data Analysis Python
import pandas as pd data = {'numbers': [1, 2, 3, 4]} df = pd.DataFrame(data) result = df.nunique(axis=2) print(result)
Attempts:
2 left
💡 Hint
Check the valid axis values for DataFrame methods.
✗ Incorrect
DataFrames have only two axes: 0 (rows) and 1 (columns). Using axis=2 is invalid and raises a ValueError about axis being out of bounds.
🚀 Application
advanced2:00remaining
Using nunique() to find columns with high cardinality
You have a DataFrame with many columns. Which code snippet correctly finds columns with more than 10 unique values?
Data Analysis Python
import pandas as pd # Assume df is predefined # Find columns with more than 10 unique values high_card_cols = ?
Attempts:
2 left
💡 Hint
nunique() by default counts unique values per column.
✗ Incorrect
df.nunique() returns a Series with unique counts per column. Filtering with > 10 selects columns with more than 10 unique values. axis=1 counts per row, which is not desired here.
🧠 Conceptual
expert2:00remaining
Understanding nunique() behavior with missing values
Consider a DataFrame column with values: [1, 2, 2, None, 3, None]. What is the output of df['col'].nunique(dropna=False)?
Data Analysis Python
import pandas as pd data = {'col': [1, 2, 2, None, 3, None]} df = pd.DataFrame(data) result = df['col'].nunique(dropna=False) print(result)
Attempts:
2 left
💡 Hint
dropna=False counts NaN as a unique value.
✗ Incorrect
The unique values are 1, 2, 3, and None (NaN). With dropna=False, NaN is counted as a unique value, so total unique count is 4.