0
0
Pandasdata~10 mins

Aggregation with agg() in Pandas - Interactive Code Practice

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

Complete the code to calculate the mean of the 'score' column using agg().

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie'], 'score': [85, 90, 78]}
df = pd.DataFrame(data)

result = df['score'].agg([1])
print(result)
Drag options to blanks, or click blank then click option'
A'mean'
B'sum'
C'max'
D'min'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sum' instead of 'mean' will add all scores instead of averaging.
Using 'max' or 'min' will return the highest or lowest score, not the average.
2fill in blank
medium

Complete the code to calculate both the minimum and maximum of the 'score' column using agg().

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob', 'Charlie'], 'score': [85, 90, 78]}
df = pd.DataFrame(data)

result = df['score'].agg([1])
print(result)
Drag options to blanks, or click blank then click option'
A['min', 'max']
B'min', 'max'
C'min'
Dmin
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a string with commas instead of a list causes errors.
Passing only one function name returns a single aggregation, not both.
3fill in blank
hard

Fix the error in the code to calculate the sum of 'score' and the mean of 'age' columns using agg().

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob'], 'score': [85, 90], 'age': [25, 30]}
df = pd.DataFrame(data)

result = df.agg({'score': [1], 'age': 'mean'})
print(result)
Drag options to blanks, or click blank then click option'
A'mean'
Bsum
C['sum']
D'sum'
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum without quotes causes a NameError.
Using a list like ['sum'] is not accepted in this context.
4fill in blank
hard

Fill both blanks to create a dictionary that calculates the mean of 'score' and the max of 'age' using agg().

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob'], 'score': [85, 90], 'age': [25, 30]}
df = pd.DataFrame(data)

result = df.agg({'score': [1], 'age': [2])
print(result)
Drag options to blanks, or click blank then click option'
A'mean'
B'max'
C'sum'
D'min'
Attempts:
3 left
💡 Hint
Common Mistakes
Using function names without quotes.
Mixing up which function applies to which column.
5fill in blank
hard

Fill all three blanks to create a dictionary that calculates the sum of 'score', the mean of 'age', and the minimum of 'score' using agg().

Pandas
import pandas as pd

data = {'name': ['Alice', 'Bob'], 'score': [85, 90], 'age': [25, 30]}
df = pd.DataFrame(data)

result = df.agg({'score': [[1], [2]], 'age': [3])
print(result)
Drag options to blanks, or click blank then click option'
A'sum'
B'mean'
C'min'
D'max'
Attempts:
3 left
💡 Hint
Common Mistakes
Using function names without quotes.
Passing a string instead of a list for multiple functions.
Mixing up the order of functions in the list.