0
0
Pandasdata~20 mins

value_counts() for frequency in Pandas - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Value Counts Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
orange    3
banana    2
apple     1
dtype: int64
B
apple     2
banana    2
orange    2
dtype: int64
C
banana    3
apple     2
orange    1
dtype: int64
D
apple     3
banana    2
orange    1
dtype: int64
Attempts:
2 left
💡 Hint
Count how many times each fruit appears in the list.
data_output
intermediate
1: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))
A4
B7
C5
D3
Attempts:
2 left
💡 Hint
Count how many different numbers appear in the Series.
🔧 Debug
advanced
2: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)
AKeyError: 'value_counts'
BAttributeError: 'DataFrame' object has no attribute 'value_counts'
CTypeError: unhashable type: 'Series'
DNo error, prints counts of unique rows
Attempts:
2 left
💡 Hint
Check if DataFrame has value_counts method and what it returns.
🚀 Application
advanced
2:00remaining
Using value_counts() with normalization
What does the 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)
AShows the counts as percentages of total values
BSorts the counts in ascending order
CFilters out values with counts less than 2
DReturns the counts as cumulative sums
Attempts:
2 left
💡 Hint
Think about what 'normalize' means in statistics.
🧠 Conceptual
expert
2:30remaining
Interpreting value_counts() output with missing values
Consider this Series with missing values:
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)
ARaises an error because None cannot be counted
BIncludes counts of missing values (NaN) in the output
CExcludes missing values and only counts non-null entries
DReplaces missing values with zero before counting
Attempts:
2 left
💡 Hint
Check what dropna=False means for counting nulls.