Cars per capita (1)
Let's stick to the cars data some more. This time you want to find out which countries have a high cars per capita figure. In other words, in which countries do many people have a car, or maybe multiple cars.
Similar to the previous example, you'll want to build up a boolean Series, that you can then use to subset the cars DataFrame to select certain observations. If you want to do this in a one-liner, that's perfectly fine!
Diese Übung ist Teil des Kurses
Intermediate Python
Anleitung zur Übung
- Select the
cars_per_capcolumn fromcarsas a Pandas Series and store it ascpc. - Use
cpcin combination with a comparison operator and500. You want to end up with a boolean Series that'sTrueif the corresponding country has acars_per_capof more than500andFalseotherwise. Store this boolean Series asmany_cars. - Use
many_carsto subsetcars, similar to what you did before. Store the result ascar_maniac. - Print out
car_maniacto see if you got it right.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)
# Create car_maniac: observations that have a cars_per_cap over 500
# Print car_maniac