0
0
Data Analysis Pythondata~15 mins

nunique() for cardinality in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Unique Values with nunique() for Cardinality
📖 Scenario: You work in a small bookstore. You have a list of books sold each day. You want to find out how many different book titles were sold.
🎯 Goal: Build a small program that counts the number of unique book titles sold using the nunique() method.
📋 What You'll Learn
Create a pandas DataFrame with a column named book_title containing the exact book titles sold.
Create a variable called unique_books_count to store the number of unique book titles using nunique().
Print the value of unique_books_count.
💡 Why This Matters
🌍 Real World
Counting unique items sold helps businesses understand product variety and customer preferences.
💼 Career
Data analysts often count unique values to summarize data and find important insights.
Progress0 / 4 steps
1
Create the sales data
Create a pandas DataFrame called sales_data with one column named book_title. The column should have these exact values in this order: 'The Alchemist', '1984', 'The Alchemist', 'Brave New World', '1984'.
Data Analysis Python
Need a hint?

Use pd.DataFrame with a dictionary where the key is 'book_title' and the value is the list of book titles.

2
Set up the variable for unique count
Create a variable called unique_books_count and set it to the number of unique book titles in sales_data['book_title'] using the nunique() method.
Data Analysis Python
Need a hint?

Use sales_data['book_title'].nunique() to count unique titles.

3
Print the number of unique book titles
Write a print() statement to display the value of unique_books_count.
Data Analysis Python
Need a hint?

Use print(unique_books_count) to show the result.

4
Explain the result
Add a print statement that shows a friendly message: print(f"Number of unique books sold: {unique_books_count}").
Data Analysis Python
Need a hint?

Use an f-string inside print() to show the message with the count.