A pivot table is a tool that summarizes data by grouping and aggregating it, making it easier to see patterns and compare values.
Click to reveal answer
beginner
What does the pandas function pivot_table() do?
It creates a pivot table from a DataFrame by grouping data based on specified columns and applying aggregation functions like sum or mean.
Click to reveal answer
intermediate
Which parameter in pivot_table() specifies the columns to group by?
The index parameter specifies the columns to group by in rows, and columns specifies columns to group by in columns.
Click to reveal answer
intermediate
How do you change the aggregation function in pivot_table()?
Use the aggfunc parameter to specify the aggregation function, like sum, mean, or a custom function.
Click to reveal answer
beginner
What happens if you don't specify the values parameter in pivot_table()?
By default, all numeric columns are aggregated and included in the pivot table.
Click to reveal answer
What parameter do you use in pivot_table() to set the rows to group by?
Aindex
Bcolumns
Cvalues
Daggfunc
✗ Incorrect
The index parameter sets the rows to group by in the pivot table.
Which aggregation function is NOT commonly used with pivot_table()?
Asum
Bmean
Ccount
Dprint
✗ Incorrect
print is not an aggregation function; sum, mean, and count are common.
If you want to see the average sales by region, which aggfunc should you use?
Amean
Bsum
Cmax
Dmin
✗ Incorrect
The mean function calculates the average.
What type of data does pivot_table() aggregate by default if values is not set?
AOnly string columns
BAll columns
COnly numeric columns
DNo columns
✗ Incorrect
By default, pivot_table() aggregates all numeric columns.
Which parameter in pivot_table() lets you add columns to group by?
Aindex
Bcolumns
Cvalues
Daggfunc
✗ Incorrect
The columns parameter adds columns to group by in the pivot table.
Explain how to create a pivot table using pandas pivot_table() to summarize sales data by region and product.
Think about grouping rows by region, columns by product, and summing sales.
You got /5 concepts.
Describe the role of the aggfunc parameter in pivot_table() and give examples of common functions used.
Aggregation means combining data to get a summary.
You got /3 concepts.
Practice
(1/5)
1. What does the index parameter do in the pivot_table() function?
easy
A. It sets the rows of the pivot table.
B. It sets the columns of the pivot table.
C. It defines the aggregation function.
D. It filters the data before pivoting.
Solution
Step 1: Understand the role of index in pivot_table()
The index parameter specifies which column(s) to use as the rows in the resulting pivot table.
Step 2: Differentiate from other parameters
The columns parameter sets the columns, aggfunc sets the aggregation, and filtering is done separately.
Final Answer:
It sets the rows of the pivot table. -> Option A
Quick Check:
index = rows [OK]
Hint: Remember: index = rows, columns = columns [OK]
Common Mistakes:
Confusing index with columns
Thinking aggfunc sets rows
Assuming index filters data
2. Which of the following is the correct syntax to create a pivot table with pandas using pivot_table()?
easy
A. pd.pivot_table(data, index='Category', columns='Year', aggfunc='sum')
B. pd.pivot(data, index='Category', columns='Year', aggfunc='sum')
C. pd.pivot_table(data, rows='Category', cols='Year', aggfunc='sum')
D. pd.pivot_table(data, groupby='Category', by='Year', aggfunc='sum')
Solution
Step 1: Recall the correct function and parameters
The correct function is pd.pivot_table() with parameters index for rows and columns for columns.
Step 2: Check each option
pd.pivot_table(data, index='Category', columns='Year', aggfunc='sum') uses correct function and parameters. Options B, C, and D use incorrect function names or parameter names.
Final Answer:
pd.pivot_table(data, index='Category', columns='Year', aggfunc='sum') -> Option A
Quick Check:
Correct function and parameters = pd.pivot_table(data, index='Category', columns='Year', aggfunc='sum') [OK]
Hint: Use index= for rows, columns= for columns in pivot_table [OK]
Common Mistakes:
Using pd.pivot instead of pd.pivot_table
Wrong parameter names like rows or cols
Mixing groupby parameters with pivot_table
3. Given the DataFrame df below:
Product Region Sales
0 A East 100
1 B East 150
2 A West 200
3 B West 250
4 A East 50
5 B West 100
A. The index parameter must be a list, not a string.
B. The aggfunc 'average' is not a valid aggregation function.
C. The values parameter cannot be a column name.
D. The columns parameter should be 'cols' instead of 'columns'.
Solution
Step 1: Check the aggfunc parameter value
The valid aggregation functions include 'mean', 'sum', 'count', etc. 'average' is not recognized by pandas.
Step 2: Verify other parameters
columns is correct, values can be a column name string, and index can be a string or list.
Final Answer:
The aggfunc 'average' is not a valid aggregation function. -> Option B
Quick Check:
aggfunc must be valid like 'mean' [OK]
Hint: Use 'mean' not 'average' for aggregation [OK]
Common Mistakes:
Using 'average' instead of 'mean'
Changing columns to cols
Passing values as non-column
5. You have a sales DataFrame with columns: Store, Product, Month, and Revenue. You want a pivot table showing total revenue per store for each product, but only for months January and February. Which code correctly creates this pivot table?
hard
A. pd.pivot_table(df, index='Store', columns='Product', values='Revenue', aggfunc='sum').loc[['January', 'February']]
B. pd.pivot_table(df, index='Store', columns='Product', values='Revenue', aggfunc='sum', filter=['January', 'February'])
C. pd.pivot_table(df, index='Month', columns='Product', values='Revenue', aggfunc='sum').query('Month in ["January", "February"]')
D. pd.pivot_table(df[df['Month'].isin(['January', 'February'])], index='Store', columns='Product', values='Revenue', aggfunc='sum')
Solution
Step 1: Filter data for January and February
Use df[df['Month'].isin(['January', 'February'])] to select only those months before pivoting.
Step 2: Create pivot table with Store as rows and Product as columns
Use index='Store', columns='Product', and sum revenue with aggfunc='sum'.
Final Answer:
pd.pivot_table(df[df['Month'].isin(['January', 'February'])], index='Store', columns='Product', values='Revenue', aggfunc='sum') -> Option D
Quick Check:
Filter first, then pivot with sum [OK]
Hint: Filter data before pivoting to limit rows [OK]
Common Mistakes:
Trying to filter inside pivot_table with invalid parameter
Filtering after pivoting causing wrong results
Using wrong index or columns
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.