-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathdriving-right-(1).py
More file actions
31 lines (25 loc) · 1.09 KB
/
driving-right-(1).py
File metadata and controls
31 lines (25 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'''
Driving right (1)
100xp
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 available on the right.
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.
Instructions
-Extract the drives_right column as a Pandas Series and store it as dr.
-Use dr, a boolean Series, to subset the cars DataFrame. Store the resulting selection in sel.
-Print sel, and assert that drives_right is True for all observations.
'''
# Import cars data
import pandas as pd
cars = pd.read_csv('cars.csv', index_col = 0)
# Extract drives_right column as Series: dr
dr = cars['drives_right']
# Use dr to subset cars: sel
sel = cars[dr]
# Print sel
print(sel)