0
0
Data Analysis Pythondata~10 mins

Pivot tables with pivot_table() in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a pivot table from the DataFrame df using the pivot_table() method.

Data Analysis Python
pivot = df.[1](index='Category', values='Sales', aggfunc='sum')
Drag options to blanks, or click blank then click option'
Apivot_table
Bgroupby
Cmerge
Dconcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using groupby instead of pivot_table.
Trying to use merge or concat which combine dataframes but don't create pivot tables.
2fill in blank
medium

Complete the code to create a pivot table that uses mean as the aggregation function.

Data Analysis Python
pivot = df.pivot_table(index='Region', values='Profit', aggfunc=[1])
Drag options to blanks, or click blank then click option'
A'mean'
B'count'
C'sum'
D'max'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' instead of 'mean' when average is needed.
Forgetting to put the aggregation function in quotes.
3fill in blank
hard

Fix the error in the code to create a pivot table with multiple index columns.

Data Analysis Python
pivot = df.pivot_table(index=[1], values='Sales', aggfunc='sum')
Drag options to blanks, or click blank then click option'
A'Region, Category'
B['Region', 'Category']
C'Region'
DRegion, Category
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single string with commas instead of a list.
Not using quotes around column names.
4fill in blank
hard

Fill both blanks to create a pivot table with Region as index and Category as columns, aggregating Profit by sum.

Data Analysis Python
pivot = df.pivot_table(index=[1], columns=[2], values='Profit', aggfunc='sum')
Drag options to blanks, or click blank then click option'
A'Region'
B'Category'
C'Sales'
D'Date'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping index and columns values.
Using a wrong column name like 'Sales' or 'Date'.
5fill in blank
hard

Fill all three blanks to create a pivot table that groups by Region and Category, aggregates Sales by mean, and fills missing values with 0.

Data Analysis Python
pivot = df.pivot_table(index=[1], columns=[2], values=[3], aggfunc='mean', fill_value=0)
Drag options to blanks, or click blank then click option'
A['Region']
B['Category']
C'Sales'
D'Profit'
Attempts:
3 left
💡 Hint
Common Mistakes
Using strings instead of lists for index or columns.
Choosing the wrong column for values.
Forgetting to set fill_value to handle missing data.