Bird
Raised Fist0
Data Analysis Pythondata~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the value_counts() function do when applied to a pandas Series?
easy
A. Calculates the average of all values in the Series
B. Counts how many times each unique value appears in the Series
C. Sorts the Series in ascending order
D. Removes duplicate values from the Series

Solution

  1. Step 1: Understand the purpose of value_counts()

    This function counts each unique value's frequency in a pandas Series.
  2. Step 2: Compare with other options

    Sorting, averaging, or removing duplicates are different operations not done by value_counts().
  3. Final Answer:

    Counts how many times each unique value appears in the Series -> Option B
  4. Quick Check:

    value_counts() = counts unique values [OK]
Hint: Remember: value_counts counts unique values frequency [OK]
Common Mistakes:
  • Thinking it sorts values instead of counting
  • Confusing it with mean or average calculation
  • Assuming it removes duplicates instead of counting
2. Which of the following is the correct syntax to get the percentage distribution of values in a pandas Series data?
easy
A. data.value_counts(percent=True)
B. data.value_counts(normalize=False)
C. data.value_counts(normalize=True)
D. data.value_counts(percentage=True)

Solution

  1. Step 1: Recall the parameter for percentage in value_counts()

    The correct parameter is normalize=True to get proportions.
  2. Step 2: Check other options for correctness

    Options with percent or percentage are invalid parameters and cause errors.
  3. Final Answer:

    data.value_counts(normalize=True) -> Option C
  4. Quick Check:

    Use normalize=True for percentages [OK]
Hint: Use normalize=True to get percentages, not percent or percentage [OK]
Common Mistakes:
  • Using wrong parameter names like percent or percentage
  • Forgetting to set normalize=True for percentages
  • Confusing normalize=False with percentage output
3. Given the pandas Series colors = pd.Series(['red', 'blue', 'red', 'green', 'blue', 'blue']), what is the output of colors.value_counts()?
medium
A. {'blue': 3, 'red': 2, 'green': 1}
B. {'red': 3, 'blue': 2, 'green': 1}
C. {'green': 1, 'red': 2, 'blue': 3}
D. {'red': 2, 'blue': 3, 'green': 1}

Solution

  1. Step 1: Count each color's occurrences

    'red' appears 2 times, 'blue' 3 times, 'green' 1 time.
  2. Step 2: Understand value_counts() output order

    It sorts counts descending by default, so 'blue' (3), 'red' (2), 'green' (1).
  3. Final Answer:

    {'blue': 3, 'red': 2, 'green': 1} -> Option A
  4. Quick Check:

    Counts sorted descending = {'blue':3,'red':2,'green':1} [OK]
Hint: value_counts() sorts by count descending automatically [OK]
Common Mistakes:
  • Ignoring the default descending sort order
  • Mixing up counts of 'red' and 'blue'
  • Assuming output is unordered or sorted alphabetically
4. What is wrong with this code snippet if df['Category'] is a pandas Series?
df.value_counts(normalize=True)
medium
A. It should be df['Category'].value_counts(normalize=True) to count values in the column
B. The parameter normalize is not valid for value_counts()
C. You cannot use value_counts() on a pandas Series
D. The code will count values but not normalize them

Solution

  1. Step 1: Identify the object value_counts() is called on

    df is a DataFrame, but value_counts() works on Series, not DataFrame directly.
  2. Step 2: Correct usage for counting a column

    Use df['Category'].value_counts(normalize=True) to count values in that column.
  3. Final Answer:

    It should be df['Category'].value_counts(normalize=True) to count values in the column -> Option A
  4. Quick Check:

    value_counts() needs a Series, not DataFrame [OK]
Hint: Call value_counts() on a Series, not on the whole DataFrame [OK]
Common Mistakes:
  • Calling value_counts() directly on DataFrame
  • Confusing DataFrame and Series methods
  • Thinking normalize is invalid parameter
5. You have a pandas Series responses with values: ['yes', 'no', 'yes', 'maybe', 'no', 'yes', 'no', 'maybe', 'yes', 'no'].
How do you get the percentage distribution of each response rounded to 2 decimals?
hard
A. responses.value_counts().round(2)
B. responses.value_counts().normalize().round(2)
C. responses.value_counts(percent=True).round(2)
D. responses.value_counts(normalize=True).round(2)

Solution

  1. Step 1: Use value_counts(normalize=True) to get percentages

    This returns the relative frequencies as decimals.
  2. Step 2: Apply round(2) to round percentages to 2 decimals

    Chaining .round(2) rounds the decimal values properly.
  3. Final Answer:

    responses.value_counts(normalize=True).round(2) -> Option D
  4. Quick Check:

    Normalize then round decimals = responses.value_counts(normalize=True).round(2) [OK]
Hint: Chain normalize=True with round(2) to get rounded percentages [OK]
Common Mistakes:
  • Using invalid parameter percent=True
  • Calling normalize() as a method instead of parameter
  • Rounding counts instead of percentages
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard