-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcitibike.py
38 lines (32 loc) · 1.41 KB
/
citibike.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
import pandas as pd
import sqlite3 as lite
from dateutil.parser import parse
import collections
import sqlite3 as lite
import datetime
con = lite.connect('citi_bike.db')
cur = con.cursor()
df = pd.read_sql_query("SELECT * FROM available_bikes ORDER BY execution_time",con,index_col='execution_time')
hour_change = collections.defaultdict(int)
for col in df.columns:
station_vals = df[col].tolist()
station_id = col[1:] #trim the "_"
station_change = 0
for k,v in enumerate(station_vals):
if k < len(station_vals) - 1:
station_change += abs(station_vals[k] - station_vals[k+1])
hour_change[int(station_id)] = station_change #convert the station id back to integer
def keywithmaxval(d):
"""Find the key with the greatest value"""
return max(d, key=lambda k: d[k])
# assign the max key to max_station
max_station = keywithmaxval(hour_change)
#query sqlite for reference information
cur.execute("SELECT id, stationname, latitude, longitude FROM citibike_reference WHERE id = ?", (max_station,))
data = cur.fetchone()
print("The most active station is station id %s at %s latitude: %s longitude: %s " % data)
print("With %d bicycles coming and going in the hour between %s and %s" % (
hour_change[max_station],
datetime.datetime.fromtimestamp(int(df.index[0])).strftime('%Y-%m-%dT%H:%M:%S'),
datetime.datetime.fromtimestamp(int(df.index[-1])).strftime('%Y-%m-%dT%H:%M:%S'),
))