-
Notifications
You must be signed in to change notification settings - Fork 6
/
mobility.py
53 lines (43 loc) · 2.26 KB
/
mobility.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
import pandas as pd
import numpy as np
def fetch_total_population_df():
train_df = pd.read_csv('training_data.csv', encoding="ISO-8859-1")
matrix = train_df.groupby(['pickup_grid_number','dropoff_grid_number']).size().unstack().fillna(0)
matrixByHours = train_df.groupby(['hour','pickup_grid_number','dropoff_grid_number']).size().unstack().fillna(0)
pickup_matrix = train_df.groupby(['hour', 'pickup_grid_number']).size().unstack().fillna(0)
dropoff_matrix = train_df.groupby(['hour', 'dropoff_grid_number']).size().unstack().fillna(0)
pickup_matrix.fillna(0, inplace=True)
dropoff_matrix.fillna(0, inplace=True)
change_obj = {}
total_population_obj = {}
for i in range(1, 2500):
try:
if i in dropoff_matrix[:].columns and i in pickup_matrix[:].columns:
change_obj[str(i)] = dropoff_matrix[:][i] - pickup_matrix[:][i]
try:
total_population_obj[str(i)] = total_population_obj[str(i-1)] + change_obj[str(i)]
except KeyError:
total_population_obj[str(i)] = change_obj[str(i)]
pass
elif i in dropoff_matrix[:].columns:
change_obj[str(i)] = dropoff_matrix[:][i]
try:
total_population_obj[str(i)] = total_population_obj[str(i-1)] + change_obj[str(i)]
except KeyError:
total_population_obj[str(i)] = change_obj[str(i)]
pass
elif i in pickup_matrix[:].columns:
change_obj[str(i)] = -pickup_matrix[:][i]
try:
total_population_obj[str(i)] = total_population_obj[str(i-1)] + change_obj[str(i)]
except KeyError:
total_population_obj[str(i)] = change_obj[str(i)]
pass
except KeyError:
pass
#net_change_df is a 24x1010 (hour by zone) matrix containing the net change in a given zone
#a positive value implies more people entering the zone then leaving
#a negative value implies more people leaving the zone then entering
net_change_df = pd.DataFrame(change_obj)
total_population_df = pd.DataFrame(total_population_obj)
return total_population_df