Lecture 1 in-class exercise solution

Lecture 1 in-class exercise solution#

Step 0#

!wget -O 2021_yellow_taxi_trips.csv --no-verbose https://data.cityofnewyork.us/resource/m6nq-qud6.csv
2024-11-06 22:42:42 URL:https://data.cityofnewyork.us/resource/m6nq-qud6.csv [136065] -> "2021_yellow_taxi_trips.csv" [1]

Step 1#

import csv


with open("2021_yellow_taxi_trips.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        print(row["trip_distance"])
2.10
0.20
14.70
10.60
4.94
1.60
4.10
5.70
9.10
2.70
6.11
1.21
7.40
1.70
0.81
1.01
0.73
1.17
0.78
1.66
0.93
1.16
2.20
3.60
19.10
5.40
3.15
4.65
4.07
14.30
3.96
0.76
0.64
3.00
10.74
2.62
13.67
6.10
0.00
0.00
2.00
1.00
1.98
1.27
1.66
0.89
4.85
1.16
1.40
2.01
3.45
3.79
17.20
3.10
2.85
1.68
0.77
0.52
0.40
1.05
5.85
0.27
3.70
3.70
16.54
2.19
4.09
3.26
1.11
2.87
4.00
1.30
2.30
0.80
7.00
0.51
1.17
3.00
2.00
1.10
0.60
2.50
3.58
1.24
4.78
1.69
1.33
8.06
2.60
1.40
2.44
1.58
3.98
1.06
1.91
4.13
4.12
2.22
7.11
1.61
1.89
1.62
3.70
6.56
0.58
1.37
2.65
1.20
0.80
0.20
1.24
1.91
0.50
0.70
1.29
4.26
0.70
4.60
1.34
6.71
1.60
1.10
9.19
8.02
4.50
9.40
1.38
0.50
0.80
5.83
1.35
4.11
0.32
2.31
1.00
0.90
0.20
0.40
11.10
8.40
2.80
1.30
2.19
2.30
0.20
2.90
0.80
3.50
6.17
0.79
5.10
5.08
6.43
14.30
1.20
0.70
3.30
15.19
3.40
2.76
0.00
0.60
1.98
2.71
1.07
0.81
2.27
1.50
0.77
1.73
1.44
1.08
4.69
9.60
1.49
1.62
1.60
1.60
2.54
3.78
1.25
2.52
1.18
3.85
5.20
0.00
5.04
2.41
2.09
1.57
2.60
1.02
1.80
6.45
7.12
4.10
2.96
7.35
2.50
2.70
1.20
2.40
0.40
1.20
6.80
1.42
0.81
1.06
1.34
1.50
1.48
3.13
2.50
0.66
0.69
3.05
0.77
4.36
2.95
4.29
4.18
3.47
0.96
0.56
15.40
1.50
4.20
8.89
6.53
1.91
17.68
5.10
0.25
2.70
1.20
1.00
1.20
6.30
1.00
1.71
0.46
2.88
4.11
1.95
1.68
5.90
1.53
1.59
5.60
0.80
5.74
3.67
1.25
7.99
0.94
0.00
1.11
0.90
1.00
1.44
1.78
1.62
0.37
0.74
1.40
6.50
7.08
5.97
0.76
3.72
0.94
1.44
0.53
1.45
1.79
2.40
20.26
3.90
2.03
7.00
1.38
1.86
3.04
1.82
0.41
6.91
1.58
1.65
1.51
1.28
10.40
6.40
17.05
2.80
3.36
5.44
0.98
1.08
33.18
2.41
2.37
3.60
2.79
0.59
1.53
0.54
3.85
7.25
2.41
1.50
2.62
0.59
1.20
0.90
14.00
0.40
1.10
2.88
1.46
2.08
9.40
1.40
1.10
15.73
1.64
3.74
0.94
3.00
7.04
1.30
1.10
6.90
1.90
2.90
0.60
1.91
2.43
7.42
6.88
3.30
1.00
0.20
0.50
1.60
1.89
1.96
0.00
8.50
0.77
1.12
4.44
6.10
3.22
2.64
19.90
0.50
2.49
1.97
8.67
1.85
1.17
0.90
6.71
7.27
0.79
2.79
1.03
1.24
0.92
3.55
5.47
9.50
6.20
1.61
7.54
2.40
0.92
1.39
2.59
2.15
1.60
1.20
2.77
1.65
1.40
1.50
1.10
1.30
0.50
2.70
1.70
11.10
9.50
1.50
2.00
0.50
3.52
3.34
2.18
1.29
10.86
1.80
0.00
2.71
4.85
0.78
0.20
3.80
2.20
2.20
0.80
5.60
1.68
12.08
3.80
0.70
2.46
5.16
1.85
5.18
5.32
0.00
1.02
4.50
3.01
3.17
3.46
1.72
2.30
1.60
3.20
1.43
6.10
7.01
0.90
2.64
0.51
0.59
1.04
0.81
0.25
2.80
1.93
2.18
1.40
3.80
0.50
1.48
3.49
0.50
2.00
5.70
0.94
2.40
2.40
0.74
0.59
1.70
3.45
12.40
5.40
1.00
12.10
2.15
2.06
0.44
1.57
3.12
1.06
5.37
5.20
0.90
1.40
14.91
2.16
1.09
5.04
0.80
2.90
1.10
3.47
3.93
0.88
3.85
2.58
1.07
1.18
2.47
4.17
1.80
0.72
2.60
4.83
0.48
2.90
6.29
0.58
1.06
8.22
0.95
0.02
0.68
12.01
5.70
10.22
0.96
0.69
2.62
1.71
0.60
5.86
0.99
8.35
7.89
1.63
0.60
1.90
2.50
5.50
3.71
2.39
1.29
12.95
12.95
7.94
2.70
3.41
0.70
1.00
2.60
17.96
0.80
0.54
2.00
0.70
1.30
4.40
1.27
3.30
1.83
1.27
9.70
2.56
5.27
0.92
2.04
3.80
0.92
2.29
0.36
1.78
0.77
0.40
1.20
1.90
1.96
0.80
2.50
3.80
8.78
1.33
1.05
5.45
7.60
3.91
1.40
1.93
3.22
1.63
17.33
0.64
1.06
1.81
1.67
2.76
0.65
3.30
2.00
1.44
1.43
3.97
2.99
5.95
1.25
0.59
0.85
3.40
2.87
1.36
5.01
6.20
19.51
2.63
2.26
1.66
2.04
0.78
3.62
1.19
1.19
0.35
1.81
2.20
1.60
8.00
0.90
2.61
1.90
3.50
1.76
4.82
3.71
1.03
2.27
4.17
0.91
3.63
4.10
0.99
0.94
6.00
2.96
0.80
5.00
6.91
3.67
5.08
1.27
0.00
0.00
3.19
1.44
0.70
0.69
2.65
1.57
2.56
2.61
3.76
8.80
2.00
2.00
1.40
6.20
2.12
3.60
0.60
2.13
0.50
2.07
1.20
2.08
4.35
5.07
2.18
0.52
3.09
27.07
0.73
1.69
3.39
7.60
4.50
1.62
1.68
3.57
0.66
0.70
0.60
2.90
2.70
0.55
5.31
5.31
9.29
3.88
1.53
4.62
3.47
2.60
2.10
4.30
3.40
2.30
0.70
1.40
1.51
2.07
1.01
3.79
0.65
0.38
0.55
1.20
2.85
2.54
2.21
0.88
3.24
4.97
4.81
3.86
1.90
7.50
12.88
16.66
0.00
16.00
5.71
15.26
2.70
3.00
0.49
7.86
0.66
2.00
0.52
1.01
3.08
0.39
3.60
7.43
5.39
3.50
2.37
0.76
0.90
2.50
1.40
0.73
3.11
6.70
1.22
0.00
2.20
0.49
18.40
6.38
1.25
2.90
0.70
19.59
2.27
1.05
3.66
1.15
4.30
1.20
3.20
3.80
1.14
7.30
5.36
1.60
3.80
4.98
0.77
1.96
0.20
0.51
6.88
1.76
0.00
0.00
0.36
7.24
3.70
0.00
4.68
1.10
32.58
2.10
0.28
0.28
3.80
3.30
0.30
3.30
1.40
0.76
1.60
0.00
1.87
0.53
2.36
1.73
4.82
0.46
1.99
3.36
4.32
1.53
1.64
0.79
1.13
0.00
0.83
4.71
0.54
3.67
1.51
0.96
0.96
0.60
2.63
2.06
1.59
2.30
0.10
1.64
1.01
2.65
0.74
1.13
1.13
2.84
2.84
1.52
1.84
1.08
1.18
1.69
2.35
6.09
3.08
0.05
1.97
1.30
0.10
1.40
1.60
2.31
5.67
4.64
1.69
2.47
0.68
1.85
2.72
3.10
2.73
0.68
2.60
4.70
2.99
11.15
1.35
0.76
3.07
3.17
1.84
1.60
5.40
0.90
1.69
2.49
0.93
3.52
7.05
2.39
10.52
1.99
4.13
8.16
1.06
4.80
1.30
5.50
3.80
0.03
1.91
1.98
1.78
1.15
2.03
10.82
5.20
2.00
0.80
11.64
1.77
1.70
7.20
5.60
3.17
1.80
6.81
3.86
1.19
1.79
0.50
9.08
0.62
21.17
2.70
1.65
1.30
12.40
0.56
3.67
10.50
2.34
6.45
11.78
1.60
0.41
0.41
1.67
3.60
0.70
1.13
0.89
1.92
2.15
1.65
2.25
7.83
9.61
9.84
2.30
4.24
2.60
1.64
2.24
0.12
7.28
5.10
4.30
2.10
2.59
0.93
0.61
0.30
6.10
5.71
3.58
3.76
1.90
2.10
1.60
1.68
6.33
23.26
4.72
7.02
3.05
1.55
3.40
2.00
6.50
1.20
3.96
2.00
1.90
8.90
4.51
0.99
2.79
13.03
2.35
0.94
2.50
1.45
2.36
8.90
3.06
0.98
2.19
2.87
0.47
0.13
1.13
1.41
1.38
0.48
3.60
6.34
6.34
0.00
9.34
7.09
8.75
0.01
0.80
3.50
4.39
2.72
2.21
3.12
4.45
1.53
3.85
0.00
0.50
2.20
3.10
2.77
2.01
3.35
3.35

Step 2#

num_trips = 0
miles = 0

with open("2021_yellow_taxi_trips.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        # convert from a string to a numeric type
        dist = float(row["trip_distance"])

        # increment the counters
        num_trips += 1
        miles += dist

print(miles / num_trips)
3.279060000000005

Step 3#

num_trips = 0
num_paid_by_card = 0

with open("2021_yellow_taxi_trips.csv") as csvfile:
    reader = csv.DictReader(csvfile)
    for row in reader:
        # increment the counters
        num_trips += 1
        if row["payment_type"] == "1":
            num_paid_by_card += 1

pct = num_paid_by_card / num_trips
print(f"{pct * 100}%")
59.0%