0
0
Pandasdata~10 mins

Iterating over groups 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 group the DataFrame by the 'Category' column.

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

Complete the code to iterate over groups and print the group name.

Pandas
for [1], group in grouped:
    print([1])
Drag options to blanks, or click blank then click option'
Agroup
Bkey
Cname
Dindex
Attempts:
3 left
💡 Hint
Common Mistakes
Using the group DataFrame instead of the group name to print.
Using a variable name that is not assigned in the loop.
3fill in blank
hard

Fix the error in the code to get the size of each group.

Pandas
sizes = grouped.[1]()
Drag options to blanks, or click blank then click option'
Asize
Bsum
Clength
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() which counts non-null entries per column, not group size.
Using non-existent methods like length().
4fill in blank
hard

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

Pandas
mean_values = { [1]: group['Value'].[2]() for [1] , group in grouped }
Drag options to blanks, or click blank then click option'
Aname
Bmean
Csum
Dkey
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name for the group key.
Using sum() instead of mean().
5fill in blank
hard

Fill all three blanks to filter groups where the mean of 'Value' is greater than 10 and create a dictionary with group names and their mean values.

Pandas
filtered_means = { [1]: group['Value'].[2]() for [1], group in grouped if group['Value'].[2]() [3] 10 }
Drag options to blanks, or click blank then click option'
Agrp
Bmean
C>
Dsum
Attempts:
3 left
💡 Hint
Common Mistakes
Using sum() instead of mean().
Using '<' instead of '>' in the condition.
Using inconsistent variable names for the group key.