📖 Scenario: Imagine you work in a small bookstore. You have a list of books sold in one day. You want to find out how many times each book was sold to understand which books are popular.
🎯 Goal: You will create a list of sold books, then use value_counts() to find the count of each book sold. Finally, you will print the distribution of book sales.
📋 What You'll Learn
Create a list called books_sold with exact book titles.
Create a Pandas Series from the list.
Use value_counts() on the Series to get the count of each book.
Print the result.
💡 Why This Matters
🌍 Real World
Counting how many times each product or item appears in sales data helps businesses understand customer preferences and manage inventory.
💼 Career
Data analysts and data scientists often use <code>value_counts()</code> to quickly summarize categorical data and find patterns.
Progress0 / 4 steps
1
Create the list of books sold
Create a list called books_sold with these exact book titles in this order: 'Python Basics', 'Data Science 101', 'Python Basics', 'Machine Learning', 'Data Science 101', 'Python Basics'.
Data Analysis Python
Hint
Remember to use square brackets [] to create a list and separate items with commas.
2
Create a Pandas Series from the list
Import the pandas library as pd. Then create a variable called books_series by converting the books_sold list into a Pandas Series using pd.Series().
Data Analysis Python
Hint
Use import pandas as pd to import Pandas. Then use pd.Series() to convert the list.
3
Use value_counts() to find the distribution
Create a variable called book_counts by applying value_counts() on the books_series variable.
Data Analysis Python
Hint
Call value_counts() directly on the Series variable.
4
Print the distribution of book sales
Print the book_counts variable to display how many times each book was sold.
Data Analysis Python
Hint
Use print(book_counts) to show the counts.
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
Step 1: Understand the purpose of value_counts()
This function counts each unique value's frequency in a pandas Series.
Step 2: Compare with other options
Sorting, averaging, or removing duplicates are different operations not done by value_counts().
Final Answer:
Counts how many times each unique value appears in the Series -> Option B
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
Step 1: Recall the parameter for percentage in value_counts()
The correct parameter is normalize=True to get proportions.
Step 2: Check other options for correctness
Options with percent or percentage are invalid parameters and cause errors.
Final Answer:
data.value_counts(normalize=True) -> Option C
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()?
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
Step 1: Identify the object value_counts() is called on
df is a DataFrame, but value_counts() works on Series, not DataFrame directly.
Step 2: Correct usage for counting a column
Use df['Category'].value_counts(normalize=True) to count values in that column.
Final Answer:
It should be df['Category'].value_counts(normalize=True) to count values in the column -> Option A
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
Step 1: Use value_counts(normalize=True) to get percentages
This returns the relative frequencies as decimals.
Step 2: Apply round(2) to round percentages to 2 decimals
Chaining .round(2) rounds the decimal values properly.
Final Answer:
responses.value_counts(normalize=True).round(2) -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.