Bird
Raised Fist0
Data Analysis Pythondata~20 mins

agg() for multiple aggregations in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
agg() Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of multiple aggregations with agg()
What is the output of this code snippet using pandas agg() for multiple aggregations?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'A': [1, 2, 3, 4],
    'B': [5, 6, 7, 8]
})

result = df.agg({'A': ['sum', 'max'], 'B': ['min', 'mean']})
print(result)
A{'A': {'sum': 10, 'max': 4}, 'B': {'min': 5, 'mean': 6.5}}
B
       A    B
sum  10.0  26.0
max   4.0   8.0
C
       A    B
sum  10.0  NaN
max   4.0  NaN
min   NaN  5.0
mean  NaN  6.5
D
A    10.0
B     5.0
dtype: float64
Attempts:
2 left
💡 Hint
agg() with dict applies each function per column and returns a DataFrame with function names as index.
❓ data_output
intermediate
1:30remaining
Number of rows in agg() result with multiple aggregations
Given this DataFrame and agg() call, how many rows does the result have?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'X': [10, 20, 30],
    'Y': [5, 15, 25]
})

result = df.agg({'X': ['min', 'max', 'mean'], 'Y': ['sum', 'mean']})
A4
B5
C3
D2
Attempts:
2 left
💡 Hint
Count unique aggregation functions used across all columns.
🔧 Debug
advanced
2:00remaining
Identify the error in agg() usage
What error does this code raise when using agg() with multiple aggregations?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1, 2, 3]})

result = df.agg({'A': 'sum', 'B': ['min', 'max']})
ATypeError: unhashable type: 'list'
BAttributeError: 'list' object has no attribute 'agg'
CValueError: No axis named B for object type DataFrame
DKeyError: 'B'
Attempts:
2 left
💡 Hint
Check if all columns in agg dict exist in DataFrame.
🚀 Application
advanced
2:30remaining
Using agg() to summarize grouped data
You have this DataFrame with groups. Which option shows the correct agg() output for multiple aggregations per group?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'Group': ['X', 'X', 'Y', 'Y'],
    'Value': [10, 20, 30, 40]
})

grouped = df.groupby('Group').agg({'Value': ['min', 'max', 'mean']})
print(grouped)
A
       Value
min  max  mean
Group               
X    10   20  15.0
Y    30   40  35.0
B
       Value
min max mean
Group               
X   10  20  15.0
Y   30  40  35.0
C
       Value
        min max mean
Group               
X         10  20  15.0
Y         30  40  35.0
D
       Value
min max mean
Group               
X  10  20  15.0
Y  30  40  35.0
Attempts:
2 left
💡 Hint
Check the multi-level column index format pandas produces for multiple aggregations.
🧠 Conceptual
expert
2:30remaining
Understanding agg() output shape with overlapping functions
If you run agg() on a DataFrame with columns 'A' and 'B' using {'A': ['sum', 'mean'], 'B': ['mean', 'max']}, how many rows and columns will the output DataFrame have?
A4 rows and 4 columns
B3 rows and 2 columns
C2 rows and 4 columns
D2 rows and 2 columns
Attempts:
2 left
💡 Hint
Rows correspond to unique aggregation functions, columns to original columns.

Practice

(1/5)
1. What does the agg() function do when used on a pandas DataFrame?
easy
A. It merges two DataFrames together.
B. It filters rows based on a condition.
C. It applies one or more aggregation functions to columns and returns summary results.
D. It sorts the DataFrame by column values.

Solution

  1. Step 1: Understand the purpose of agg()

    agg() is used to apply aggregation functions like sum, mean, count on DataFrame columns.
  2. Step 2: Identify what agg() returns

    It returns a summary DataFrame or Series with the aggregated results for specified columns.
  3. Final Answer:

    It applies one or more aggregation functions to columns and returns summary results. -> Option C
  4. Quick Check:

    agg() = summary aggregation [OK]
Hint: Remember: agg() summarizes columns with functions fast [OK]
Common Mistakes:
  • Thinking agg() filters or sorts data
  • Confusing agg() with merge or join
  • Expecting agg() to modify original data
2. Which of the following is the correct syntax to apply agg() with multiple functions on a DataFrame column named 'sales'?
easy
A. df['sales'].agg(['sum', 'mean'])
B. df.agg('sales', ['sum', 'mean'])
C. df.agg({'sales': 'sum', 'mean'})
D. df['sales'].agg('sum', 'mean')

Solution

  1. Step 1: Check correct agg() syntax for multiple functions

    To apply multiple functions on one column, pass a list of function names inside agg() like agg(['sum', 'mean']).
  2. Step 2: Validate the syntax options

    df['sales'].agg(['sum', 'mean']) uses df['sales'].agg(['sum', 'mean']), which is correct. Others have wrong argument order or structure.
  3. Final Answer:

    df['sales'].agg(['sum', 'mean']) -> Option A
  4. Quick Check:

    Multiple functions in list inside agg() [OK]
Hint: Use a list of functions inside agg() for multiple aggregations [OK]
Common Mistakes:
  • Passing multiple functions as separate arguments
  • Using dict without proper key-value pairs
  • Wrong argument order in agg()
3. Given the DataFrame:
import pandas as pd
data = {'A': [1, 2, 3, 4], 'B': [5, 6, 7, 8]}
df = pd.DataFrame(data)
result = df.agg({'A': ['sum', 'max'], 'B': ['min', 'mean']})
print(result)

What is the output of print(result)?
medium
A. A B sum 10.0 NaN max 4.0 NaN min NaN 5.0 mean NaN 6.5
B. A B sum 10 5 max 4 8 min 1 5 mean 2.5 6.5
C. A B sum 10 26 max 4 8 min NaN NaN mean NaN NaN
D. A B sum 10 5 max 4 7 min 5 6.5 mean 2.5 6.5

Solution

  1. Step 1: Calculate aggregations for column 'A'

    Sum of A = 1+2+3+4 = 10, Max of A = 4.
  2. Step 2: Calculate aggregations for column 'B'

    Min of B = 5, Mean of B = (5+6+7+8)/4 = 6.5.
  3. Step 3: Understand agg() output format

    Output is a DataFrame with aggregation functions as index and columns as 'A' and 'B'. For functions not applied to a column, NaN appears.
  4. Final Answer:

    A B sum 10.0 NaN max 4.0 NaN min NaN 5.0 mean NaN 6.5 -> Option A
  5. Quick Check:

    agg() with dict of lists returns DataFrame with NaNs for missing aggregations [OK]
Hint: Calculate each agg function separately, then check output shape [OK]
Common Mistakes:
  • Mixing up min and mean values
  • Confusing rows and columns in output
  • Expecting all functions for all columns
4. What is wrong with this code snippet?
df.agg({'col1': 'sum', 'col2': ['mean', 'max']})

Assuming df has columns 'col1' and 'col2'.
medium
A. You cannot mix single function strings and lists in the same agg dictionary.
B. agg() requires all functions to be passed as a list, even if only one.
C. The column names must be passed as a list, not a dictionary.
D. The syntax is correct; no error will occur.

Solution

  1. Step 1: Check if mixing single function and list is allowed in agg()

    agg() accepts a dict where values can be a string (single function) or list (multiple functions).
  2. Step 2: Confirm syntax validity

    Passing {'col1': 'sum', 'col2': ['mean', 'max']} is valid and works correctly.
  3. Final Answer:

    The syntax is correct; no error will occur. -> Option D
  4. Quick Check:

    agg() accepts mixed single and multiple functions [OK]
Hint: agg() dict values can be string or list of functions [OK]
Common Mistakes:
  • Assuming all dict values must be lists
  • Trying to pass columns as list instead of dict keys
  • Confusing agg() with apply() syntax
5. You have a DataFrame df with columns 'score' and 'group'. You want to get the mean and standard deviation of 'score' for each 'group'. Which code correctly uses agg() to do this after grouping?
hard
A. df.groupby('group')['score'].agg('mean', 'std')
B. df.groupby('group').agg({'score': ['mean', 'std']})
C. df.agg({'score': ['mean', 'std']}, groupby='group')
D. df.groupby('score')['group'].agg(['mean', 'std'])

Solution

  1. Step 1: Understand grouping and aggregation

    To get stats per group, first group by 'group', then aggregate 'score' with mean and std.
  2. Step 2: Check each option

    df.groupby('group')['score'].agg('mean', 'std') passes functions as separate arguments, incorrect syntax. df.groupby('group').agg({'score': ['mean', 'std']}) groups by 'group' and applies agg on 'score' column correctly. df.agg({'score': ['mean', 'std']}, groupby='group') has invalid syntax. df.groupby('score')['group'].agg(['mean', 'std']) groups by 'score' which is wrong.
  3. Step 3: Choose best option

    df.groupby('group').agg({'score': ['mean', 'std']}) is the correct way to get mean and std of 'score' per 'group'.
  4. Final Answer:

    df.groupby('group').agg({'score': ['mean', 'std']}) -> Option B
  5. Quick Check:

    Group by then agg with dict for columns [OK]
Hint: Group first, then agg with dict for multiple functions per column [OK]
Common Mistakes:
  • Grouping by wrong column
  • Passing groupby as argument to agg()
  • Aggregating wrong columns
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard