python提取地铁出行数据OD
求助各位大神:现有一天的地铁出行数据,需要用python提取出每一位乘客的出行交通起始点。
求教各位大神,感激不尽啊。
卡号, 刷卡时间, 线路站点, 刷卡金额, 优惠类型 1001, 2019-07-01, 临平, 3, 0.9 1002, 2019-07-01, 临平, 3, 0.9 1003, 2019-07-01, 临平, 3, 0.9
import csv with open('data.csv') as f: data = csv.reader(f) for e in data: print(e[0], e[2])
ID,Date,time,Address,class,cost 5770,2016/9/1,6:15:44,13号线大渡河路,地铁,0 7017,2016/9/1,8:03:11,8号线耀华路,地铁,0 7017,2016/9/1,8:43:12,13号线大渡河路,地铁,4 5770,2016/9/1,9:25:22,13号线金沙江路,地铁,7 2345,2016/9/1,12:15:44,13号线大渡河路,地铁,0 2345,2016/9/1,12:25:22,13号线金沙江路,地铁,2
# -*- coding:UTF-8 -*- import csv from datetime import timedelta import copy def td_from_str(time_str): t = [int(s) for s in time_str.split(':')] return timedelta(hours=t[0], minutes=t[1], seconds=t[2]) # ID,Date,time,Address,class,cost class OriginDestination: def __init__(self, id, date, timestr, address, type, cost): self.id = id self.date = date self.time = td_from_str(timestr) self.address = address self.it_type = type self.cost = int(cost) def description(self): total_seconds = self.time.seconds h = total_seconds // 3600 m = (total_seconds % 3600) // 60 s = total_seconds % 60 t = '%d:%02d:%02d' % (h, m, s) return "%s,%s,%s,%s,%s,%d" % (self.id, self.date, t, self.address, self.it_type, self.cost) def before7(e): bt = timedelta(hours=7) if e.cost == 0 and e.time < bt: print("\t%s" % e.description()) with open('before7.csv', 'w') as f: f.write(e.description()) def after10(e): at = timedelta(hours=10) if e.cost == 0 and e.time > at: print("\t%s" % e.description()) with open('after10.csv', 'w') as f: f.write(e.description()) def hour2(es): N = len(es) for start in range(N): if es[start].cost != 0: continue for end in range(start + 1, N): if es[start].id == es[end].id: if es[end].time - es[start].time > timedelta(hours=2): through_time = (es[end].time - es[start].time).seconds td = "\t用时%d小时%d分%d秒" % (through_time // 3600, through_time % 3600 // 60, through_time % 60) print(td, end=': ') print(es[start].description(), end=' -> ') print(es[end].description()) with open('hour2.csv', 'w') as f: it = es[start].description() + ',separation,' + es[end].description() f.write(it) break if __name__ == '__main__': ods = [] with open('data.csv') as f: data = csv.reader(f) next(data) for e in data: od = OriginDestination(*e) ods.append(copy.deepcopy(od)) print('出发时间在7点以前:') for eb in ods: before7(eb) print('出发时间在10点以后:') for ea in ods: after10(ea) print('出行时长在2h以上:') hour2(ods)
[此贴子已经被作者于2019-7-28 13:08编辑过]