0
0
Pandasdata~15 mins

nunique() for unique counts in Pandas - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Unique Values with pandas nunique()
📖 Scenario: You work at a small bookstore. You have a list of books sold each day. You want to find out how many unique book titles were sold each day.
🎯 Goal: Build a small program using pandas to count unique book titles sold per day using the nunique() function.
📋 What You'll Learn
Create a pandas DataFrame with sales data including 'day' and 'book_title' columns.
Create a variable to hold the day to analyze.
Use nunique() to count unique book titles sold on that day.
Print the count of unique book titles sold on the chosen day.
💡 Why This Matters
🌍 Real World
Counting unique items sold or recorded is common in sales, inventory, and customer data analysis.
💼 Career
Data analysts and scientists often use <code>nunique()</code> to quickly find how many distinct values exist in a dataset column.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales with two columns: 'day' and 'book_title'. Use these exact rows: 'Monday', 'Book A', 'Monday', 'Book B', 'Monday', 'Book A', 'Tuesday', 'Book C', 'Tuesday', 'Book B'.
Pandas
Need a hint?

Use pd.DataFrame with a dictionary for columns.

2
Set the day to analyze
Create a variable called selected_day and set it to the string 'Monday'.
Pandas
Need a hint?

Just assign the string 'Monday' to selected_day.

3
Count unique book titles sold on the selected day
Create a variable called unique_count that stores the number of unique book_title values in sales where the day equals selected_day. Use the nunique() function.
Pandas
Need a hint?

Use sales.loc[sales['day'] == selected_day, 'book_title'].nunique() to count unique titles.

4
Print the unique book count
Print the value of unique_count.
Pandas
Need a hint?

Use print(unique_count) to show the result.