Complete the code to create a pivot table from the DataFrame df using the pivot_table() method.
pivot = df.[1](index='Category', values='Sales', aggfunc='sum')
groupby instead of pivot_table.merge or concat which combine dataframes but don't create pivot tables.The pivot_table() method is used to create pivot tables in pandas. Here, it groups data by 'Category' and sums the 'Sales'.
Complete the code to create a pivot table that uses mean as the aggregation function.
pivot = df.pivot_table(index='Region', values='Profit', aggfunc=[1])
'sum' instead of 'mean' when average is needed.The aggfunc parameter defines how to aggregate data. Using 'mean' calculates the average.
Fix the error in the code to create a pivot table with multiple index columns.
pivot = df.pivot_table(index=[1], values='Sales', aggfunc='sum')
The index parameter accepts a list of column names to group by multiple columns. It must be a list like ['Region', 'Category'].
Fill both blanks to create a pivot table with Region as index and Category as columns, aggregating Profit by sum.
pivot = df.pivot_table(index=[1], columns=[2], values='Profit', aggfunc='sum')
index and columns values.The index is the row grouping, here 'Region'. The columns parameter creates columns for each 'Category'.
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.
pivot = df.pivot_table(index=[1], columns=[2], values=[3], aggfunc='mean', fill_value=0)
index or columns.values.fill_value to handle missing data.The index groups rows by 'Region', columns groups columns by 'Category', and values selects 'Sales' to aggregate by mean. Missing values are replaced by 0.