-
Notifications
You must be signed in to change notification settings - Fork 1
/
divvy.py
96 lines (81 loc) · 2.55 KB
/
divvy.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Download data from https://divvy-tripdata.s3.amazonaws.com/202206-divvy-tripdata.zip
# If you are working with a CSV file with commas as
# part of the fields, then a regular string split
# with a comma delimiter wil not be sufficient.
# In this case, open the file as a CSV file.
'''
import csv
def analyze():
with open("AgeDataset-V1.csv", "r") as f:
reader = csv.reader(f, delimiter=",")
for fields in reader:
#your code here
'''
'''
Column 0: ride_id
Column 1: rideable_type
Column 2: started_at
Column 3: ended_at
Column 4: start_station_name
Column 5: start_station_id
Column 6: end_station_name
Column 7: end_station_id
Column 8: start_lat
Column 9: start_lng
Column 10: end_lat
Column 11: end_lng
Column 12: member_casual
'''
# Count the number of classic and electric bike rides in the dataset.
def analyze():
f = open("202206-divvy-tripdata/202206-divvy-tripdata.csv")
f.readline()
classic_bike_total = 0
electric_bike_total = 0
for line in f.readlines():
fields = line.split(",")
bike_type = fields[1]
if bike_type == "classic_bike":
classic_bike_total += 1
elif bike_type == "electric_bike":
electric_bike_total += 1
return (classic_bike_total, electric_bike_total)
# Count the number of round trips, AKA when a bike started and ended at the same station.
def round_trips():
f = open("202206-divvy-tripdata/202206-divvy-tripdata.csv")
count = 0
for line in f.readlines():
fields = line.split(",")
start_station = fields[4]
end_station = fields[6]
if start_station == end_station:
count += 1
return count
# Get all of the unique divvy stations.
def unique_stations():
f = open("202206-divvy-tripdata/202206-divvy-tripdata.csv")
stations = set()
f.readline()
for line in f.readlines():
fields = line.split(",")
start_station = fields[4]
end_station = fields[6]
stations.add(start_station)
stations.add(end_station)
print(len(stations))
sorted_stations = sorted(list(stations))
return sorted_stations
def rides_dict():
f = open('202206-divvy-tripdata.csv')
f.readline()
start_dict = {}
for line in f.readlines():
fields = line.split(',')
start_station = fields[4]
if start_station not in start_dict:
start_dict[start_station] = 1
else:
start_dict[start_station] += 1
start_dict.pop('')
sorted_start_dict = sorted(start_dict.items(), key=lambda x: x[1], reverse=True)
return sorted_start_dict[:10]