0
0
Pandasdata~10 mins

Grouping by multiple columns 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 'city' and calculate the mean of 'temperature'.

Pandas
grouped = df.groupby([1])['temperature'].mean()
Drag options to blanks, or click blank then click option'
A'city'
B'humidity'
C'date'
D'temperature'
Attempts:
3 left
💡 Hint
Common Mistakes
Grouping by 'temperature' instead of 'city' will not group locations.
Forgetting quotes around the column name causes errors.
2fill in blank
medium

Complete the code to group the DataFrame by both 'city' and 'date' columns.

Pandas
grouped = df.groupby([1]).sum()
Drag options to blanks, or click blank then click option'
A['humidity']
B['city']
C['city', 'date']
D['temperature']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single string instead of a list for multiple columns.
Grouping by columns not present in the DataFrame.
3fill in blank
hard

Fix the error in the code to group by 'city' and 'date' and calculate the mean temperature.

Pandas
result = df.groupby([1]).mean()['temperature']
Drag options to blanks, or click blank then click option'
A'city', 'date'
B['city', 'date']
C('city', 'date')
D'city date'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing multiple columns as a single string separated by commas.
Using parentheses instead of square brackets for the list.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps each city to the count of dates where temperature is above 20.

Pandas
counts = {city: df[(df['city'] == city) & (df['temperature'] [1] 20)]['date'].[2]() for city in df['city'].unique()}
Drag options to blanks, or click blank then click option'
A>
Bcount
Cmean
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' for the temperature condition.
Using 'mean' instead of 'count' to count dates.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps each city to the average humidity on days when temperature is below 15.

Pandas
avg_humidity = {city: df[(df['city'] == city) & (df['temperature'] [1] 15)]['[2]'].[3]() for city in df['city'].unique()}
Drag options to blanks, or click blank then click option'
A<
Bhumidity
Cmean
Dtemperature
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<' for the temperature condition.
Selecting 'temperature' column instead of 'humidity'.
Using 'count' instead of 'mean' to calculate average.