-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_information_sprottenflotte.py
More file actions
462 lines (362 loc) · 22.2 KB
/
get_information_sprottenflotte.py
File metadata and controls
462 lines (362 loc) · 22.2 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
import pandas as pd
# Some functions to extract some data from Sprottenflotte. It will most often return a dictonary with the
# station_ids as the key.
def get_total_number_of_bikes(sprottenflotte_df):
'''Get the total number of bikes available for Sprottenflotte.'''
last_update_list = sprottenflotte_df["last_update"].to_list()
dict_timestamp = {}
max_number_of_bikes = []
for timestamp in last_update_list:
if timestamp not in dict_timestamp:
dict_timestamp[timestamp] = True
result_df = sprottenflotte_df.loc[sprottenflotte_df['last_update'] == timestamp]
# print(result_df["Number_of_Bikes"].sum())
max_number_of_bikes.append(result_df["Number_of_Bikes"].sum())
# print(max_number_of_bikes)
return max(max_number_of_bikes)
def frequency_of_station_rentals_total(sprottenflotte_df):
'''Returns the frequency of usage of a station over the total amount of time as a dictonary.'''
last_update_list = sprottenflotte_df["last_update"].unique()
station_id_list = sprottenflotte_df["Station_ID"].unique()
length_of_time = len(last_update_list)
frequencies_stations = {}
for station in station_id_list:
station_id_df = sprottenflotte_df.loc[sprottenflotte_df["Station_ID"] == station]
last_reported_list = station_id_df["last_reported"].unique()
frequencies_stations[station] = len(last_reported_list) / length_of_time
return frequencies_stations
def avg_number_of_bike_per_day(sprottenflotte_df):
'''Returns the average number of bikes available at the stationper day as dictonary.'''
avg_number_of_bikes = {}
first_update = sprottenflotte_df["last_update"].values[0]
one_day = first_update + 86400
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
number_bikes_per_day = []
if station == 26355:
one_day_copy = 1678212530 + 21070 + 86400
first_update_copy = 1678212530
else:
one_day_copy = one_day
first_update_copy = first_update
while one_day_copy < last_update:
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < one_day_copy) ]
entry = result_df["Number_of_Bikes"].to_list()
number_bikes_per_day.append(sum(entry) / len(entry))
first_update_copy = one_day_copy
one_day_copy += 86400
avg_number_of_bikes[station] = sum(number_bikes_per_day) / len(number_bikes_per_day)
return avg_number_of_bikes
def avg_usage_of_station_per_day(sprottenflotte_df):
'''Returns the average usage of a station per day as a dictanory.'''
avg_usage_of_station = {}
first_update = sprottenflotte_df["last_update"].values[0]
one_day = first_update + 86400
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
usage_of_station_per_day = []
if station == 26355:
one_day_copy = 1678212530 + 21070 + 86400
first_update_copy = 1678212530
else:
one_day_copy = one_day
first_update_copy = first_update
while one_day_copy < last_update:
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < one_day_copy) ]
reported = result_df["last_reported"].unique()
usage_of_station_per_day.append(len(reported))
first_update_copy = one_day_copy
one_day_copy += 86400
avg_usage_of_station[station] = sum(usage_of_station_per_day) / len(usage_of_station_per_day)
return avg_usage_of_station
def get_returning(number_of_bikes: list):
'''
Given a list of values, it returns a number, which is calculated by comparing two values next to each other.
The number is the returned number of bikes to that station.
'''
index = 0
returned = 0
while index < len(number_of_bikes) - 1:
if number_of_bikes[index] < number_of_bikes[index + 1]:
returned += number_of_bikes[index + 1] - number_of_bikes[index]
index += 1
return returned
# same as get_returning, just the other way around
def get_renting(number_of_bikes: list):
'''
Given a list of values, it returns a number, which is calculated by comparing two values next to each other.
The number is the retented number of bikes to that station.
'''
index = 0
renting = 0
while index < len(number_of_bikes) - 1:
if number_of_bikes[index] > number_of_bikes[index + 1]:
renting += number_of_bikes[index] - number_of_bikes[index + 1]
index += 1
return renting
def avg_returning_bike_of_station_per_day(sprottenflotte_df):
'''Returns the average of the returning bikes to a station per day as a dictonary.'''
avg_returning_bike_of_station = {}
first_update = sprottenflotte_df["last_update"].values[0]
one_day = first_update + 86400
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
number_of_bikes_returning= []
if station == 26355:
one_day_copy = 1678212530 + 21070 + 86400
first_update_copy = 1678212530
else:
one_day_copy = one_day
first_update_copy = first_update
while one_day_copy < last_update:
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < one_day_copy) ]
number_of_bikes = result_df["Number_of_Bikes"].to_list()
number_of_bikes_returning.append(get_returning(number_of_bikes))
first_update_copy = one_day_copy
one_day_copy += 86400
avg_returning_bike_of_station[station] = sum(number_of_bikes_returning) / len(number_of_bikes_returning)
return avg_returning_bike_of_station
def avg_renting_bike_of_station_per_day(sprottenflotte_df):
'''Return the average of how often a bike is rented from a station per day as a dictonary.'''
avg_renting_bike_of_station = {}
first_update = sprottenflotte_df["last_update"].values[0]
one_day = first_update + 86400
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
number_of_bikes_renting= []
if station == 26355:
one_day_copy = 1678212530 + 21070 + 86400
first_update_copy = 1678212530
else:
one_day_copy = one_day
first_update_copy = first_update
while one_day_copy < last_update:
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < one_day_copy) ]
number_of_bikes = result_df["Number_of_Bikes"].to_list()
number_of_bikes_renting.append(get_renting(number_of_bikes))
first_update_copy = one_day_copy
one_day_copy += 86400
avg_renting_bike_of_station[station] = sum(number_of_bikes_renting) / len(number_of_bikes_renting)
return avg_renting_bike_of_station
def avg_number_of_bike_per_half_hour(sprottenflotte_df):
'''Returns the average numer of bike available at a station per half an hour as dictonary.'''
avg_number_of_bikes = {}
first_update = sprottenflotte_df["last_update"].values[0]
half_hour = first_update + 1800
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
number_bikes_per_half_hour = []
if station == 26355:
half_hour_copy = 1678212530 + 1800
first_update_copy = 1678212530
else:
half_hour_copy = half_hour
first_update_copy = first_update
while half_hour_copy < last_update:
if station == 26355:
if not (first_update_copy < 1678374768 and first_update_copy > 1678212837):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
entry = result_df["Number_of_Bikes"].to_list()
number_bikes_per_half_hour.append(sum(entry) / len(entry))
elif not ((first_update_copy < 1678180903 and first_update_copy > 1678168785) or (first_update_copy > 1678249874 and first_update_copy < 1678264138)):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
entry = result_df["Number_of_Bikes"].to_list()
number_bikes_per_half_hour.append(sum(entry) / len(entry))
first_update_copy = half_hour_copy
half_hour_copy += 1800
avg_number_of_bikes[station] = sum(number_bikes_per_half_hour) / len(number_bikes_per_half_hour)
return avg_number_of_bikes
def avg_usage_of_station_per_half_hour(sprottenflotte_df):
'''Returns the average usage of a station per half hour as a dictonary.'''
avg_usage_of_station = {}
first_update = sprottenflotte_df["last_update"].values[0]
half_hour = first_update + 1800
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
usage_of_station_per_half_hour = []
if station == 26355:
half_hour_copy = 1678212530 + 1800
first_update_copy = 1678212530
else:
half_hour_copy = half_hour
first_update_copy = first_update
while half_hour_copy < last_update:
if station == 26355:
if not (first_update_copy < 1678374768 and first_update_copy > 1678212837):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
reported = result_df["last_reported"].unique()
usage_of_station_per_half_hour.append(len(reported))
elif not ((first_update_copy < 1678180903 and first_update_copy > 1678168785) or (first_update_copy > 1678249874 and first_update_copy < 1678264138)):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
reported = result_df["last_reported"].unique()
usage_of_station_per_half_hour.append(len(reported))
first_update_copy = half_hour_copy
half_hour_copy += 1800
avg_usage_of_station[station] = sum(usage_of_station_per_half_hour) / len(usage_of_station_per_half_hour)
return avg_usage_of_station
def avg_returning_bike_of_station_per_half_hour(sprottenflotte_df):
'''Returns the average of how often a bike is returned to a station per half hour as a dictonary.'''
avg_returning_bike_of_station = {}
first_update = sprottenflotte_df["last_update"].values[0]
half_hour = first_update + 1800
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
number_of_bikes_returning = []
if station == 26355:
half_hour_copy = 1678212530 + 1800
first_update_copy = 1678212530
else:
half_hour_copy = half_hour
first_update_copy = first_update
while half_hour_copy < last_update:
if station == 26355:
if not (first_update_copy < 1678374768 and first_update_copy > 1678212837):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
number_of_bikes = result_df["Number_of_Bikes"].to_list()
number_of_bikes_returning.append(get_returning(number_of_bikes))
elif not ((first_update_copy < 1678180903 and first_update_copy > 1678168785) or (first_update_copy > 1678249874 and first_update_copy < 1678264138)):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
number_of_bikes = result_df["Number_of_Bikes"].to_list()
number_of_bikes_returning.append(get_returning(number_of_bikes))
first_update_copy = half_hour_copy
half_hour_copy += 1800
avg_returning_bike_of_station[station] = sum(number_of_bikes_returning) / len(number_of_bikes_returning)
return avg_returning_bike_of_station
def avg_renting_bike_of_station_per_half_hour(sprottenflotte_df):
'''Returns the average of how often a bike is renting at a station per half hour as a dictonary.'''
avg_renting_bike_of_station = {}
first_update = sprottenflotte_df["last_update"].values[0]
half_hour = first_update + 1800
station_id_list = sprottenflotte_df["Station_ID"].unique()
last_update = sprottenflotte_df["last_update"].values[-1]
for station in station_id_list:
number_of_bikes_renting = []
if station == 26355:
half_hour_copy = 1678212530 + 1800
first_update_copy = 1678212530
else:
half_hour_copy = half_hour
first_update_copy = first_update
while half_hour_copy < last_update:
if station == 26355:
if not (first_update_copy < 1678374768 and first_update_copy > 1678212837):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
number_of_bikes = result_df["Number_of_Bikes"].to_list()
number_of_bikes_renting.append(get_renting(number_of_bikes))
if not ((first_update_copy < 1678180903 and first_update_copy > 1678168785) or (first_update_copy > 1678249874 and first_update_copy < 1678264138)):
result_df = sprottenflotte_df.loc[ (sprottenflotte_df["Station_ID"] == station) &
(sprottenflotte_df["last_update"] >= first_update_copy) &
(sprottenflotte_df["last_update"] < half_hour_copy) ]
number_of_bikes = result_df["Number_of_Bikes"].to_list()
number_of_bikes_renting.append(get_renting(number_of_bikes))
first_update_copy = half_hour_copy
half_hour_copy += 1800
avg_renting_bike_of_station[station] = sum(number_of_bikes_renting) / len(number_of_bikes_renting)
return avg_renting_bike_of_station
def get_station_of_kiel(sprottenflotte_with_lat_lon_df):
'''Get only the station in Kiel, not the station in Eckernförde etc.'''
result = sprottenflotte_with_lat_lon_df.loc[ (sprottenflotte_with_lat_lon_df["lon"] > 10.0) &
((sprottenflotte_with_lat_lon_df["lon"] < 10.26))
]
return result
if __name__ == '__main__':
sprottenflotte_df = pd.read_csv(r"./data/sprottenflotte_data.csv")
sprottenflotte_names_df = pd.read_csv(r"./general_data/sprottenflotte_map_stationID_to_stationName.csv")
number_of_bikes_total = get_total_number_of_bikes(sprottenflotte_df)
total_frequency_of_station_rentals = frequency_of_station_rentals_total(sprottenflotte_df)
avg_number_of_bike_day = avg_number_of_bike_per_day(sprottenflotte_df)
avg_usage_of_station_day = avg_usage_of_station_per_day(sprottenflotte_df)
avg_returning_bike_of_station_day = avg_returning_bike_of_station_per_day(sprottenflotte_df)
avg_renting_bike_of_station_day = avg_renting_bike_of_station_per_day(sprottenflotte_df)
station_id = sprottenflotte_df["Station_ID"].unique()
# prepare total frequencies for dataframe
total_frequency_of_station_rentals_list = []
for key in total_frequency_of_station_rentals:
total_frequency_of_station_rentals_list.append(total_frequency_of_station_rentals[key])
# prepare average number of bikes per day for dataframe
avg_number_of_bike_day_list = []
for key in avg_number_of_bike_day:
avg_number_of_bike_day_list.append(avg_number_of_bike_day[key])
# prepare average usage of station per day for dataframe
avg_usage_of_station_day_list = []
for key in avg_usage_of_station_day:
avg_usage_of_station_day_list.append(avg_usage_of_station_day[key])
avg_returning_bike_of_station_day_list = []
for key in avg_returning_bike_of_station_day:
avg_returning_bike_of_station_day_list.append(avg_returning_bike_of_station_day[key])
avg_renting_bike_of_station_day_list = []
for key in avg_renting_bike_of_station_day:
avg_renting_bike_of_station_day_list.append(avg_renting_bike_of_station_day[key])
sprottenflotte_attributes_df = pd.DataFrame(station_id ,columns=["Station_ID"])
# append all the attribute with values to the dataframes
sprottenflotte_attributes_df["Frequency_of_Usage"] = total_frequency_of_station_rentals_list
sprottenflotte_attributes_df["Avg_Number_of_Bikes"] = avg_number_of_bike_day_list
sprottenflotte_attributes_df["Avg_Usage_of_Station"] = avg_usage_of_station_day_list
sprottenflotte_attributes_df["Avg_Returning_Bike_of_Station"] = avg_returning_bike_of_station_day_list
sprottenflotte_attributes_df["Avg_Renting_Bike_of_Station"] = avg_renting_bike_of_station_day_list
sprottenflotte_attributes_df["Total_numbers_of_Bikes"] = number_of_bikes_total
# merge dataframes with names togehter on key "Station_ID"
result_day = pd.merge(sprottenflotte_attributes_df, sprottenflotte_names_df, on="Station_ID")
# save Dataframe as csv
result_day.to_csv("./general_data/sprottenflotte_attributes_day.csv")
print("First Dataframe saved.")
avg_number_of_bike_per_half = avg_number_of_bike_per_half_hour(sprottenflotte_df)
avg_usage_of_station_per_half = avg_usage_of_station_per_half_hour(sprottenflotte_df)
avg_returning_bike_of_station_per_half = avg_returning_bike_of_station_per_half_hour(sprottenflotte_df)
avg_renting_bike_of_station_per_half = avg_renting_bike_of_station_per_half_hour(sprottenflotte_df)
# prepare average number of bikes per day for dataframe
avg_number_of_bike_half_hour_list = []
for key in avg_number_of_bike_per_half:
avg_number_of_bike_half_hour_list.append(avg_number_of_bike_per_half[key])
# prepare average usage of station per day for dataframe
avg_usage_of_station_per_half_list = []
for key in avg_usage_of_station_per_half:
avg_usage_of_station_per_half_list.append(avg_usage_of_station_per_half[key])
avg_returning_bike_of_station_per_half_list = []
for key in avg_returning_bike_of_station_per_half:
avg_returning_bike_of_station_per_half_list.append(avg_returning_bike_of_station_per_half[key])
avg_renting_bike_of_station_per_half_list = []
for key in avg_renting_bike_of_station_per_half:
avg_renting_bike_of_station_per_half_list.append(avg_renting_bike_of_station_per_half[key])
sprottenflotte_attributes_half_hour_df = pd.DataFrame(station_id ,columns=["Station_ID"])
# append all the attribute with values to the dataframes
sprottenflotte_attributes_half_hour_df["Frequency_of_Usage"] = total_frequency_of_station_rentals_list
sprottenflotte_attributes_half_hour_df["Avg_Number_of_Bikes"] = avg_number_of_bike_half_hour_list
sprottenflotte_attributes_half_hour_df["Avg_Usage_of_Station"] = avg_usage_of_station_per_half_list
sprottenflotte_attributes_half_hour_df["Avg_Returning_Bike_of_Station"] = avg_returning_bike_of_station_per_half_list
sprottenflotte_attributes_half_hour_df["Avg Renting_Bike_of_Station"] = avg_renting_bike_of_station_per_half_list
sprottenflotte_attributes_half_hour_df["Total_numbers_of_Bikes"] = number_of_bikes_total
# merge dataframes with names togehter on key "Station_ID"
result_half_hour = pd.merge(sprottenflotte_attributes_half_hour_df, sprottenflotte_names_df, on="Station_ID")
# save Dataframe as csv
result_half_hour.to_csv("./general_data/sprottenflotte_attributes_half_hour.csv")
print("Done.")