Challenge - 5 Problems
Value Counts Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of value_counts() on a simple Series
What is the output of the following code?
import pandas as pd s = pd.Series(['apple', 'banana', 'apple', 'orange', 'banana', 'banana']) result = s.value_counts() print(result)
Pandas
import pandas as pd s = pd.Series(['apple', 'banana', 'apple', 'orange', 'banana', 'banana']) result = s.value_counts() print(result)
Attempts:
2 left
💡 Hint
Count how many times each fruit appears in the list.
✗ Incorrect
The value_counts() method counts how many times each unique value appears. 'banana' appears 3 times, 'apple' 2 times, and 'orange' once.
❓ data_output
intermediate1:30remaining
Number of unique values counted by value_counts()
Given the Series below, how many unique values does
value_counts() return?import pandas as pd s = pd.Series([10, 20, 10, 30, 20, 20, 40]) counts = s.value_counts()
Pandas
import pandas as pd s = pd.Series([10, 20, 10, 30, 20, 20, 40]) counts = s.value_counts() print(len(counts))
Attempts:
2 left
💡 Hint
Count how many different numbers appear in the Series.
✗ Incorrect
The unique values are 10, 20, 30, and 40, so value_counts() returns 4 counts.
🔧 Debug
advanced2:00remaining
Identify the error in using value_counts() on a DataFrame
What error will this code produce?
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 2, 3], 'B': ['x', 'y', 'x', 'z']})
result = df.value_counts()
print(result)Pandas
import pandas as pd df = pd.DataFrame({'A': [1, 2, 2, 3], 'B': ['x', 'y', 'x', 'z']}) result = df.value_counts() print(result)
Attempts:
2 left
💡 Hint
Check if DataFrame has value_counts method and what it returns.
✗ Incorrect
Since pandas 1.1.0, DataFrame has a value_counts() method that counts unique rows. So no error occurs and it prints counts of unique rows.
🚀 Application
advanced2:00remaining
Using value_counts() with normalization
What does the
Given:
normalize=True option do in value_counts()?Given:
import pandas as pd s = pd.Series(['cat', 'dog', 'cat', 'bird', 'dog', 'dog']) result = s.value_counts(normalize=True) print(result)
Pandas
import pandas as pd s = pd.Series(['cat', 'dog', 'cat', 'bird', 'dog', 'dog']) result = s.value_counts(normalize=True) print(result)
Attempts:
2 left
💡 Hint
Think about what 'normalize' means in statistics.
✗ Incorrect
normalize=True returns the relative frequencies (proportions) instead of raw counts.
🧠 Conceptual
expert2:30remaining
Interpreting value_counts() output with missing values
Consider this Series with missing values:
What does the output show compared to
import pandas as pd s = pd.Series(['red', 'blue', None, 'red', 'green', None, 'blue']) result = s.value_counts(dropna=False) print(result)
What does the output show compared to
dropna=True?Pandas
import pandas as pd s = pd.Series(['red', 'blue', None, 'red', 'green', None, 'blue']) result = s.value_counts(dropna=False) print(result)
Attempts:
2 left
💡 Hint
Check what dropna=False means for counting nulls.
✗ Incorrect
dropna=False includes NaN values in the counts, so missing values are counted and shown in the output.