0
0
Data Analysis Pythondata~30 mins

Pivot tables with pivot_table() in Data Analysis Python - Mini Project: Build & Apply

Choose your learning style9 modes available
Pivot tables with pivot_table()
📖 Scenario: You work in a small store that sells different types of fruits. You have a list of sales data showing how many fruits were sold each day. You want to summarize this data to see the total sales for each fruit and each day.
🎯 Goal: Build a pivot table using pandas to summarize total fruit sales by fruit type and day.
📋 What You'll Learn
Create a pandas DataFrame with sales data
Create a variable for the pivot table configuration
Use pivot_table() to summarize total sales by fruit and day
Print the resulting pivot table
💡 Why This Matters
🌍 Real World
Pivot tables help summarize and analyze sales data quickly, making it easier to understand trends and totals.
💼 Career
Data analysts and business intelligence professionals use pivot tables to create reports and insights from raw data.
Progress0 / 4 steps
1
Create the sales data DataFrame
Import pandas as pd and create a DataFrame called sales_data with these exact columns and rows:
{'Fruit': ['Apple', 'Banana', 'Apple', 'Banana', 'Apple'], 'Day': ['Monday', 'Monday', 'Tuesday', 'Tuesday', 'Wednesday'], 'Quantity': [10, 5, 7, 3, 8]}
Data Analysis Python
Hint

Use pd.DataFrame() with a dictionary to create the data.

2
Set up the pivot table configuration
Create a variable called pivot_config that is a dictionary with keys index set to 'Fruit', columns set to 'Day', and values set to 'Quantity'.
Data Analysis Python
Hint

Use a dictionary with keys index, columns, and values.

3
Create the pivot table
Use pd.pivot_table() with sales_data and the pivot_config dictionary to create a pivot table called sales_pivot. Use the aggfunc parameter set to sum to add quantities.
Data Analysis Python
Hint

Use pd.pivot_table() with the keys from pivot_config and aggfunc=sum.

4
Print the pivot table
Print the sales_pivot pivot table to see the total quantities sold by fruit and day.
Data Analysis Python
Hint

Use print(sales_pivot) to display the pivot table.