📖 Scenario: You work in a small store that sells fruits. You have a list of sales records showing the fruit type, the day of the week, and the quantity sold. You want to organize this data to see the total quantity sold for each fruit on each day.
🎯 Goal: Create a pivot table using pandas that shows the total quantity of each fruit sold per day of the week.
📋 What You'll Learn
Create a pandas DataFrame with the given sales data
Create a list of days to use as columns in the pivot table
Use pandas pivot_table with aggregation function sum to calculate total quantities
Print the resulting pivot table
💡 Why This Matters
🌍 Real World
Pivot tables help organize and summarize sales data to understand trends and performance by categories and time periods.
💼 Career
Data analysts and business intelligence professionals use pivot tables to quickly summarize and report data for decision making.
Progress0 / 4 steps
1
Create the sales data DataFrame
Create a pandas DataFrame called sales_data with these exact columns and rows: Fruit: ['Apple', 'Banana', 'Apple', 'Banana', 'Apple', 'Banana'] Day: ['Monday', 'Monday', 'Tuesday', 'Tuesday', 'Wednesday', 'Wednesday'] Quantity: [10, 5, 15, 7, 10, 8]
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Create the list of days for columns
Create a list called days with these exact values: ['Monday', 'Tuesday', 'Wednesday']
Pandas
Hint
Just create a list with the three day names in order.
3
Create the pivot table with sum aggregation
Create a pivot table called pivot_table from sales_data using pd.pivot_table. Use Fruit as the index, Day as the columns, Quantity as the values, and sum as the aggregation function. Also, use the days list as the columns order.
Pandas
Hint
Use pd.pivot_table with the right parameters and reorder columns with .reindex(columns=days).
4
Print the pivot table
Print the pivot_table variable to see the total quantity of each fruit sold per day.
Pandas
Hint
Use print(pivot_table) to display the pivot table.
Practice
(1/5)
1. What does the pivot_table function in pandas primarily do?
easy
A. It rearranges data to summarize and compare values using aggregation.
B. It sorts data alphabetically by column names.
C. It deletes duplicate rows from the DataFrame.
D. It converts a DataFrame into a list of dictionaries.
Solution
Step 1: Understand the purpose of pivot_table
The pivot_table function rearranges data by creating a new table that summarizes data values grouped by specified keys.
Step 2: Recognize aggregation role
It uses aggregation functions like sum or mean to combine data values in the new table.
Final Answer:
It rearranges data to summarize and compare values using aggregation. -> Option A
Quick Check:
pivot_table = rearrange + aggregate [OK]
Hint: Pivot tables summarize data by grouping and aggregating [OK]
Common Mistakes:
Confusing pivot_table with sorting or filtering
Thinking it deletes duplicates
Believing it converts DataFrame to list
2. Which of the following is the correct syntax to create a pivot table with pandas to calculate the mean of 'Sales' grouped by 'Region'?
easy
A. pd.pivot_table(data, columns='Region', values='Sales', aggfunc='sum')
B. pd.pivot_table(data, index='Region', values='Sales', aggfunc='mean')
C. data.pivot_table(index='Region', 'Sales', aggfunc='mean')
D. pd.pivot(data, index='Region', values='Sales', aggfunc='mean')
Solution
Step 1: Identify correct function and parameters
The correct function is pd.pivot_table with parameters: data, index='Region', values='Sales', and aggfunc='mean'.
Step 2: Check each option
pd.pivot_table(data, index='Region', values='Sales', aggfunc='mean') matches the correct syntax. pd.pivot(data, index='Region', values='Sales', aggfunc='mean') uses pd.pivot which does not support aggfunc. data.pivot_table('Sales', 'Region', aggfunc='mean') has wrong method call syntax. pd.pivot_table(data, columns='Region', values='Sales', aggfunc='sum') uses columns instead of index and wrong aggregation.
Final Answer:
pd.pivot_table(data, index='Region', values='Sales', aggfunc='mean') -> Option B
A. No error; output shows count of points per team.
B. aggfunc='count' is invalid; should use 'sum' instead.
C. Missing columns parameter causes runtime error.
D. DataFrame missing 'columns' argument for pivot_table.
Solution
Step 1: Understand aggfunc='count'
The 'count' aggregation counts non-null entries in 'Points' for each 'Team'.
Step 2: Check code correctness
The code is valid and will output the count of 'Points' per team without error.
Final Answer:
No error; output shows count of points per team. -> Option A
Quick Check:
aggfunc='count' counts entries correctly [OK]
Hint: aggfunc='count' counts non-null values, no columns needed [OK]
Common Mistakes:
Thinking 'count' is invalid aggfunc
Expecting columns parameter is mandatory
Confusing count with sum
5. You have sales data with columns: 'Store', 'Product', 'Month', and 'Revenue'. You want to create a pivot table that shows the total revenue per 'Store' for each 'Month', but only for months where total revenue exceeds 1000. Which approach correctly achieves this?
hard
A. Filter original data for revenue > 1000, then pivot with index='Store', columns='Month', values='Revenue', aggfunc='sum'.
B. Use pivot_table with index='Month', columns='Store', values='Revenue', aggfunc='mean', then filter values > 1000.
C. Pivot with index='Store', columns='Product', values='Revenue', aggfunc='sum', then filter rows with sum > 1000.
D. Create pivot with index='Store', columns='Month', values='Revenue', aggfunc='sum', then filter columns with sum > 1000.
Solution
Step 1: Create pivot table by Store and Month
Use pivot_table with index='Store', columns='Month', values='Revenue', and aggfunc='sum' to get total revenue per store per month.
Step 2: Filter months by total revenue
Since filtering by months with revenue > 1000 is needed, filter columns after pivot since columns represent months.
Step 3: Choose correct approach
Create pivot with index='Store', columns='Month', values='Revenue', aggfunc='sum', then filter columns with sum > 1000, which creates the pivot and then filters columns (months) where total revenue exceeds 1000, matching the requirement.
Final Answer:
Create pivot with index='Store', columns='Month', values='Revenue', aggfunc='sum', then filter columns with sum > 1000. -> Option D
Quick Check:
Pivot then filter sums > 1000 [OK]
Hint: Pivot first, then filter aggregated sums to meet condition [OK]
Common Mistakes:
Filtering raw data before pivot loses aggregation
Using mean instead of sum for totals
Mixing up index and columns parameters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.