0
0
Data Analysis Pythondata~20 mins

value_counts() for distributions in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Value Counts Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of value_counts() with dropna=false
What is the output of the following code snippet?
Data Analysis Python
import pandas as pd
s = pd.Series(['apple', 'banana', 'apple', None, 'banana', None, 'apple'])
result = s.value_counts(dropna=False)
print(result)
A
apple     3
banana    2
dtype: int64
B
apple     2
banana    2
NaN       3
dtype: int64
C
apple     3
banana    2
NaN       1
dtype: int64
D
apple     3
banana    2
NaN       2
dtype: int64
Attempts:
2 left
💡 Hint
Remember that dropna=False includes missing values in the count.
data_output
intermediate
1:30remaining
Number of unique categories counted by value_counts()
Given the following Series, how many unique categories will value_counts() return?
Data Analysis Python
import pandas as pd
s = pd.Series(['cat', 'dog', 'cat', 'bird', 'dog', 'dog', 'cat', 'fish'])
counts = s.value_counts()
print(len(counts))
A3
B4
C5
D8
Attempts:
2 left
💡 Hint
Count how many different animals appear in the list.
🔧 Debug
advanced
2:00remaining
Identify the error in value_counts() usage
What error will this code raise?
Data Analysis Python
import pandas as pd
s = pd.Series([1, 2, 2, 3, 3, 3])
result = s.value_counts(normalize=True, sort=False, ascending=True)
print(result)
AValueError: Cannot specify both sort=False and ascending=True
BTypeError: value_counts() got an unexpected keyword argument 'ascending'
CNo error; outputs normalized counts sorted ascending
DAttributeError: 'Series' object has no attribute 'value_counts'
Attempts:
2 left
💡 Hint
Check the allowed parameters and their combinations for value_counts().
visualization
advanced
2:30remaining
Bar plot from value_counts() output
Which option shows the correct bar plot code to visualize the counts of categories in a Series s?
Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt
s = pd.Series(['red', 'blue', 'red', 'green', 'blue', 'blue'])
A
s.value_counts().plot(kind='bar')
plt.show()
B
s.plot(kind='bar')
plt.show()
C
s.value_counts().plot.barh()
plt.show()
D
s.value_counts().plot(kind='line')
plt.show()
Attempts:
2 left
💡 Hint
value_counts() returns counts; plot them as bars for category frequencies.
🚀 Application
expert
3:00remaining
Calculate relative frequency distribution with value_counts()
You have a Series s with categorical data. How do you calculate the relative frequency (percentage) of each category and round it to 2 decimals?
Data Analysis Python
import pandas as pd
s = pd.Series(['A', 'B', 'A', 'C', 'B', 'B', 'A', 'C', 'C', 'C'])
As.value_counts(normalize=True).round(2) * 100
Bs.value_counts().apply(lambda x: round(x / len(s), 2))
C(s.value_counts(normalize=True) * 100).round(2)
Ds.value_counts().div(len(s)).round(2) * 100
Attempts:
2 left
💡 Hint
Normalize counts to get proportions, multiply by 100 for percentage, then round.