-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformSpeedFraction.py
40 lines (33 loc) · 1.29 KB
/
transformSpeedFraction.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
import pandas as pd
import numpy as np
from pandas import ExcelWriter
xls = pd.ExcelFile('forTransformation_speedFraction.xlsx')
sheets = xls.sheet_names
result = pd.DataFrame()
for sheet in sheets:
data = xls.parse(sheet)
data['Hour'] = data['Hour'].fillna(method = 'ffill')
idx = pd.MultiIndex.from_arrays([data['Hour'],data['Speed Fractions Range']])
data = data.set_index(idx)
data = data.drop(columns = ['Hour', 'Speed Fractions Range'])
data = data.stack(0,1)
data = data.reset_index()
data.columns = ['Hour', 'Speed Fractions Range', 'Vehicle Type', 'Speed Fraction']
data['Road Type'] = sheet
try:
result = pd.concat([data, result])
except:
result = data
cols = result.columns.tolist()
cols = cols[-1:] + cols[:-1]
result = result[cols]
result = result.sort_values(['Road Type', 'Hour'])
result.to_csv('speedFraction.csv', index=False)
#############################################
# For EPD template
############################################
writer = ExcelWriter("speedFraction_EPD.xlsx")
for vehicleType in result['Vehicle Type'].unique():
data = pd.pivot_table(result, values= 'Speed Fraction', index= ['Speed Fractions Range'], columns= ['Hour'], aggfunc = np.sum)
#print(data)
data.to_excel(writer,'{}'.format(vehicleType))