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!
This exercise is part of the course
Intermediate Python
Exercise instructions
- 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.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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