0
0
Data Analysis Pythondata~30 mins

filter() for group-level filtering in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Filter Groups Using filter() in Python
📖 Scenario: You work in a small bookstore. You have a list of sales records. Each record shows the genre of the book sold and the number of copies sold that day.You want to find which genres sold more than 20 copies in total.
🎯 Goal: Build a Python program that groups sales by genre and uses filter() to keep only genres with total sales above 20.
📋 What You'll Learn
Create a list of sales records as tuples with genre and copies sold
Create a function to sum copies sold per genre
Use filter() to keep genres with total sales above a threshold
Print the filtered genres and their total sales
💡 Why This Matters
🌍 Real World
Filtering grouped data is common in sales analysis, customer segmentation, and many business reports.
💼 Career
Data analysts and scientists often need to filter groups of data based on summary statistics to find important trends.
Progress0 / 4 steps
1
Create the sales data list
Create a list called sales with these exact tuples: ("Fiction", 10), ("Science", 15), ("Fiction", 12), ("History", 8), ("Science", 10), ("History", 15).
Data Analysis Python
Hint

Use a list of tuples. Each tuple has a genre string and an integer for copies sold.

2
Create a dictionary to sum sales by genre
Create a dictionary called genre_totals. Use a for loop to add copies sold for each genre from sales. Initialize missing genres with 0 before adding.
Data Analysis Python
Hint

Use a dictionary to keep totals. Check if genre is in dictionary before adding.

3
Use filter() to keep genres with total sales above 20
Create a function called above_20 that takes a genre_total tuple and returns True if the total sales (second item) is greater than 20. Then use filter() with above_20 on genre_totals.items() and convert the result to a list called filtered_genres.
Data Analysis Python
Hint

The function gets a tuple like ("Fiction", 22). Check if the second value is greater than 20.

4
Print the filtered genres and their total sales
Write a print() statement to display filtered_genres.
Data Analysis Python
Hint

Just print the filtered_genres list to see the genres with sales above 20.