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.
This exercise is part of the course
Working with Geospatial Data in Python
Exercise instructions
- Using
groupby()
, group the restaurants by the type of restaurants, and calculate the size of each group. Call the resulttype_counts
. - Print the resulting Series.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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(____)