Driving right (1)
Remember that cars
dataset, containing the cars per 1000 people (cars_per_cap
) and whether people drive right (drives_right
) for different countries (country
)? The code that imports this data in CSV format into Python as a DataFrame is included in the script.
In the video, you saw a step-by-step approach to filter observations from a DataFrame based on boolean arrays. Let's start simple and try to find all observations in cars
where drives_right
is True
.
drives_right
is a boolean column, so you'll have to extract it as a Series and then use this boolean Series to select observations from cars
.
This exercise is part of the course
Intermediate Python
Exercise instructions
- Extract the
drives_right
column as a Pandas Series and store it asdr
. - Use
dr
, a boolean Series, to subset thecars
DataFrame. Store the resulting selection insel
. - Print
sel
, and assert thatdrives_right
isTrue
for all observations.
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)
# Extract drives_right column as Series: dr
# Use dr to subset cars: sel
# Print sel