-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsv_operation.py
315 lines (203 loc) · 8.54 KB
/
csv_operation.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
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
# sorting csv file in the order of vehicle_no
from __future__ import division
import pandas as pd
import csv
import time
from datetime import datetime
from statistics import mean, median
from collections import namedtuple
from contextlib import closing
import MySQLdb
MyStruct = namedtuple("vehicle", "vehicle_no loading_out_time delivery_status")
def sort(file = "Bhiwadi_Data.csv", params = ['origin', 'destination', 'vehicle_no', 'loading_out_time', 'odometer_km', 'ist_timestamp']):
df = pd.read_csv(file)
df = df.sort_values(by = params)
# df = df.sort_values(by = 'vehicle_no')
df.to_csv('Full_List_sorted.csv', index=False)
return "Full_List_sorted.csv"
def get_toll_visit_count(rows):
tolls = {}
for row in rows :
if row[2] not in tolls:
tolls[row[2]] = 1;
else:
tolls[row[2]] = tolls[row[2]] + 1;
return tolls
def get_data_from_file(filename, vehicle_nos = None, delivery_status = None):
fields = []
rows = []
with open(filename, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# extracting field names through first row
fields = csvreader.next()
# extracting each data row one by one
for row in csvreader:
rows.append(row)
# get total number of rows
# print("Total no. of rows: %d"%(len(rows)))
return rows
def append_time_taken(filename):
vehicle_no = None
loading_out_time = None
time_taken = []
distance_travelled = []
delivery_status = []
late_by_hrs = []
distance_reference = None
with open(filename, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
fields = csvreader.next()
# extracting each data row one by one
for row in csvreader:
if (not vehicle_no) or (vehicle_no != row[3]) or (not loading_out_time) or (loading_out_time != row[4]):
# print "\n\n-----new vehicle-----"
# print row[3] + " " + row[4]
vehicle_no = row[3]
loading_out_time = row[4]
distance_travelled.append(0)
distance_reference = float(row[5])
else:
distance_travelled.append(float(row[5]) - distance_reference)
time_taken_temp = datetime.strptime(row[6], "%Y-%m-%d %H:%M:%S") - datetime.strptime(row[4], "%Y-%m-%d %H:%M:%S")
# store time_taken value as int in hrs
time_taken.append(time_taken_temp.total_seconds()/3600)
late_by_hrs_temp = (datetime.strptime(row[11], "%Y-%m-%d %H:%M:%S") - datetime.strptime(row[8], "%Y-%m-%d %H:%M:%S")) - (datetime.strptime(row[10], "%Y-%m-%d %H:%M:%S") - datetime.strptime(row[9], "%Y-%m-%d %H:%M:%S"))
late_by_hrs_temp = late_by_hrs_temp.total_seconds() / 3600
late_by_hrs.append(late_by_hrs_temp)
if late_by_hrs_temp >= round(12.0000, 3):
delivery_status.append("late")
elif late_by_hrs_temp <= round(-12.0000, 3):
delivery_status.append("early")
else:
delivery_status.append("on-time")
df = pd.read_csv(filename)
new_column = pd.DataFrame({'late_by_hrs': late_by_hrs, 'delivery_status':delivery_status, 'time_taken': time_taken, 'distance_travelled': distance_travelled})
df = df.merge(new_column, left_index = True, right_index = True)
df.to_csv(filename)
# delete rows where some cloumns are empty
def del_row(file1 = "Bhiwadi_Data.csv"):
file2 = "edited.csv"
with open(file1, 'rb') as inp, open(file2, 'wb') as out:
writer = csv.writer(out)
for row in csv.reader(inp):
if row[5] != "":
writer.writerow(row)
return file2
def get_vehicle_nos(filename = "consigner_trips.csv", origin = "Jamshedpur", destination = "Bhiwadi"):
vehicle_nos = []
with open(filename, 'r') as csvfile:
# creating a csv reader object
csvreader = csv.reader(csvfile)
# extracting field names through first row
fields = csvreader.next()
# extracting each data row one by one
for row in csvreader:
if (row[2] == origin) and (row[3] == destination):
# print row[2], row[3], row[12]
# print ""
temp = MyStruct(row[1], row[8], row[12])
vehicle_nos.append(temp)
# get total number of rows
# print("Total no. of rows: %d"%(csvreader.line_num))
return vehicle_nos
# input is data read from csv file
# [orgin][destination][toll_both] = [instances][min][max][avg][median]
def create_hash(rows):
time_taken_dict = {}
dis_travel_dict = {}
origin = rows[0][1]
destination = rows[0][2]
toll_both = rows[0][3]
time_taken_dict[origin] = {}
time_taken_dict[origin][destination] = {}
time_taken_dict[origin][destination][toll_both] = []
dis_travel_dict[origin] = {}
dis_travel_dict[origin][destination] = {}
dis_travel_dict[origin][destination][toll_both] = []
for row in rows:
if (origin != row[1]):
origin = row[1]
time_taken_dict[origin] = {}
dis_travel_dict[origin] = {}
destination = row[2]
time_taken_dict[origin][destination] = {}
dis_travel_dict[origin][destination] = {}
toll_both = row[3]
time_taken_dict[origin][destination][toll_both] = []
dis_travel_dict[origin][destination][toll_both] = []
elif (destination != row[2]):
destination = row[2]
time_taken_dict[origin][destination] = {}
dis_travel_dict[origin][destination] = {}
toll_both = row[3]
time_taken_dict[origin][destination][toll_both] = []
dis_travel_dict[origin][destination][toll_both] = []
elif (toll_both != row[3]):
toll_both = row[3]
time_taken_dict[origin][destination][toll_both] = []
dis_travel_dict[origin][destination][toll_both] = []
dis_travel_dict[origin][destination][toll_both].append(float(row[15]))
time_taken_dict[origin][destination][toll_both].append(float(row[17]))
return dis_travel_dict, time_taken_dict
# input is dict object that contains list to do stat on it
# and table name toll_time_stat or toll_dis_stat
def insert_stat(dict, table_name):
db = MySQLdb.connect(user = "root", passwd = "root", db = "eta_stat")
# delete previous data
with closing(db.cursor()) as cursor:
query = "delete from " + table_name + ";"
cursor.execute(query)
db.commit()
print(cursor._last_executed)
# find stat values
for origin in dict:
for destination in dict[origin]:
for toll_both in dict[origin][destination]:
max_val = max(dict[origin][destination][toll_both])
min_val = min(dict[origin][destination][toll_both])
instances = len(dict[origin][destination][toll_both])
avg_val = sum(dict[origin][destination][toll_both])/instances
median_val = median(dict[origin][destination][toll_both])
# insert values in db
with closing(db.cursor()) as cursor:
query = "insert into " + table_name + " (origin, destination, toll_both, instances, min, max, avg, median) values (%s, %s, %s, %s, %s, %s, %s, %s);"
cursor.execute(query, (origin, destination, toll_both, instances, min_val, max_val, avg_val, median_val))
db.commit()
print(cursor._last_executed)
# raise
db.close()
# input is rows read from csv file
# output is initialize a table in db
# add vehicle info in table
def insert_vehicle_stat(rows):
db = MySQLdb.connect(user = "root", passwd = "root", db = "eta_stat")
# delete previous data
with closing(db.cursor()) as cursor:
query = "delete from vehicle_stat;"
cursor.execute(query)
db.commit()
print(cursor._last_executed)
for row in rows:
with closing(db.cursor()) as cursor:
query = "insert into vehicle_stat (origin, destination, vehicle_no, loading_out_time, delivery_status, toll_both, time_taken) values (%s, %s, %s, %s, %s, %s, %s);"
cursor.execute(query, (row[1], row[2], row[4], row[5], row[14], row[3], row[17]))
db.commit()
print(cursor._last_executed)
db.close()
# fields=[origin', 'destination', 'toll_both', 'vehicle_no', 'loading_out_time', 'odometer_km', 'ist_timestamp', 'distance_from_toll', 'loading_in_time', 'start_date', 'eta', 'unloading_in_time', 'slug', 'delivery_status', 'distance_travelled', 'late_by_hrs', 'time_taken']
# index with one
# zero index is some unnamed id created by append func
filename = "time_between_two_tolls(3).csv"
# delete rows s.t. odometer_km is null
filename = del_row(filename)
filename = sort(filename)
append_time_taken(filename)
# sort on the basis of toll plaza
sort(file = filename, params = ['origin', 'destination', 'toll_both'])
rows = get_data_from_file(filename)
insert_vehicle_stat(rows)
dis_travel_dict, time_taken_dict = create_hash(rows)
insert_stat(time_taken_dict, "toll_time_stat")
insert_stat(dis_travel_dict, "toll_dis_stat")