LoslegenKostenlos loslegen

Using pandas functionality: groupby

This course will focus on the spatial functionality of GeoPandas, but don't forget that we still have a dataframe, and all functionality you know from Pandas is still applicable.

In this exercise, we will recap a common functionality: the groupby operation. You may want to use this operation when you have a column containing groups, and you want to calculate a statistic for each group. In the groupby() method, you pass the column that contains the groups. On the resulting object, you can then call the method you want to calculate for each group. In this exercise, we want to know the size of each group of type of restaurants.

We refer to the course on Manipulating DataFrames with pandas for more information and exercises on this groupby operation.

Diese Übung ist Teil des Kurses

Working with Geospatial Data in Python

Kurs anzeigen

Anleitung zur Übung

  • Using groupby(), group the restaurants by the type of restaurants, and calculate the size of each group. Call the result type_counts.
  • Print the resulting Series.

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

# Load the restaurants data
restaurants = geopandas.read_file("paris_restaurants.geosjon")

# Calculate the number of restaurants of each type
type_counts = restaurants.groupby(____).____()

# Print the result
print(____)
Code bearbeiten und ausführen