0
0
Data Analysis Pythondata~10 mins

agg() for multiple aggregations 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 calculate the mean of the 'score' column using agg().

Data Analysis Python
import pandas as pd

data = {'score': [10, 20, 30, 40, 50]}
df = pd.DataFrame(data)
result = df['score'].agg([1])
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 'sum' instead of 'mean' will give the total, not the average.
Forgetting to put the method name in quotes causes an error.
2fill in blank
medium

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

Data Analysis Python
import pandas as pd

data = {'score': [5, 15, 25, 35, 45]}
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'
D['mean', 'sum']
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single string with commas instead of a list causes an error.
Using parentheses instead of square brackets for the list.
3fill in blank
hard

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

Data Analysis Python
import pandas as pd

data = {'score': [2, 4, 6, 8, 10]}
df = pd.DataFrame(data)
result = df['score'].agg([1])
print(result)
Drag options to blanks, or click blank then click option'
A'sum', 'mean'
B('sum', 'mean')
C['sum', 'mean']
D'sum mean'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing multiple strings separated by commas without a list causes a syntax error.
Using a tuple instead of a list is not accepted by agg().
4fill in blank
hard

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

Data Analysis Python
import pandas as pd

data = {'score': [1, 2, 3], 'age': [20, 30, 40]}
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', 'sum']
B'mean'
C'max'
D['max']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a list for multiple aggregations.
Using a list for a single aggregation unnecessarily.
5fill in blank
hard

Fill all three blanks to create a dictionary for agg() that calculates the sum of 'sales', the mean of 'profit', and the max of 'quantity'.

Data Analysis Python
import pandas as pd

data = {'sales': [100, 200, 300], 'profit': [10, 20, 30], 'quantity': [1, 2, 3]}
df = pd.DataFrame(data)
result = df.agg({'sales': [1], 'profit': [2], 'quantity': [3])
print(result)
Drag options to blanks, or click blank then click option'
A'sum'
B'mean'
C'max'
D'min'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up aggregation functions for columns.
Forgetting to put aggregation names in quotes.