0
0
Data Analysis Pythondata~10 mins

groupby() basics 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 group the DataFrame by the 'Category' column.

Data Analysis Python
grouped = df.groupby([1])
Drag options to blanks, or click blank then click option'
A'Category'
B'Value'
C'Date'
D'Index'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name that does not exist in the DataFrame.
Forgetting to put the column name inside quotes.
2fill in blank
medium

Complete the code to calculate the mean of each group after grouping by 'Category'.

Data Analysis Python
mean_values = df.groupby('Category').[1]()
Drag options to blanks, or click blank then click option'
Asum
Bmax
Ccount
Dmean
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() instead of mean().
Forgetting the parentheses after the function name.
3fill in blank
hard

Fix the error in the code to get the size of each group after grouping by 'Category'.

Data Analysis Python
group_sizes = df.groupby('Category').[1]
Drag options to blanks, or click blank then click option'
Asize()
Bsize
Ccount()
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using size without parentheses, which returns a method object.
Using count() which counts non-null values per column, not total rows.
4fill in blank
hard

Fill both blanks to create a dictionary with group names as keys and the sum of 'Value' as values.

Data Analysis Python
result = {group: data['Value'].[1]() for group, data in df.groupby([2])}
Drag options to blanks, or click blank then click option'
Asum
Bmean
C'Category'
D'Value'
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean instead of sum for aggregation.
Grouping by the wrong column name.
5fill in blank
hard

Fill all three blanks to create a dictionary with group names as keys and the max 'Value' for each group.

Data Analysis Python
result = {group: data[[1]].[2]() for group, data in df.groupby([3])}
Drag options to blanks, or click blank then click option'
A'Value'
Bmax
C'Category'
D'Date'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong column name in the first blank.
Using min or mean instead of max.
Grouping by a column other than 'Category'.