| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4816 人关注过本帖
标题:python提取地铁出行数据OD
只看楼主 加入收藏
侧耳倾听rs
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-7-27
结帖率:0
收藏
已结贴  问题点数:20 回复次数:7 
python提取地铁出行数据OD
求助各位大神:
现有一天的地铁出行数据,需要用python提取出每一位乘客的出行交通起始点。
求教各位大神,感激不尽啊。
搜索更多相关主题的帖子: python 提取 行数 取出 感激不尽 
2019-07-27 09:57
TysonKoothra
Rank: 5Rank: 5
等 级:职业侠客
威 望:7
帖 子:71
专家分:374
注 册:2018-10-21
收藏
得分:10 
数据存放在什么类型文件里面?Excel表格文件,csv 文件,还是别的什么?
2019-07-27 11:05
侧耳倾听rs
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-7-27
收藏
得分:0 
回复 2楼 TysonKoothra
csv文件里面呢
2019-07-27 11:28
TysonKoothra
Rank: 5Rank: 5
等 级:职业侠客
威 望:7
帖 子:71
专家分:374
注 册:2018-10-21
收藏
得分:0 
其中的一行是什么样的,也就是想知道这个数据结构是什么样的。
2019-07-27 11:32
侧耳倾听rs
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2019-7-27
收藏
得分:0 
回复 4楼 TysonKoothra
数据包括 卡号 刷卡时间 线路站点 刷卡金额和优惠类型
2019-07-27 11:33
TysonKoothra
Rank: 5Rank: 5
等 级:职业侠客
威 望:7
帖 子:71
专家分:374
注 册:2018-10-21
收藏
得分:0 
我写一个例子,比如数据存放在 data.csv文件,用 app.py文件处理数据,提取出每一位乘客的出行交通起始点

假如 data.csv内容如下
程序代码:
卡号, 刷卡时间, 线路站点, 刷卡金额, 优惠类型
1001, 2019-07-01, 临平, 3, 0.9
1002, 2019-07-01, 临平, 3, 0.9
1003, 2019-07-01, 临平, 3, 0.9


app.py可以这么写
程序代码:
import csv

with open('data.csv') as f:
    data = csv.reader(f)
    for e in data:
        print(e[0], e[2])
2019-07-27 11:45
幻梵
Rank: 2
等 级:论坛游民
帖 子:1
专家分:10
注 册:2019-7-27
收藏
得分:10 
回复 6楼 TysonKoothra
ID,Date,time,Address,class,cost
7017,2016/9/1,8:03:11,8号线耀华路,地铁,0
7017,2016/9/1,8:43:12,13号线大渡河路,地铁,4
2345,2016/9/1,12:15:44,13号线大渡河路,地铁,0
2345,2016/9/1,12:25:22,13号线金沙江路,地铁
数据是这样的,我想筛选出出发时间在7点以前,10点以后,以及出行时长在2h以上的人群(整行数据),但是代码不会写。。。
2019-07-27 22:02
TysonKoothra
Rank: 5Rank: 5
等 级:职业侠客
威 望:7
帖 子:71
专家分:374
注 册:2018-10-21
收藏
得分:0 
我使用这样一组实验数据,为了使结果明显一些
程序代码:
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


下面进行数据处理 app.py
程序代码:
# -*- 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编辑过]

2019-07-28 13:06
快速回复:python提取地铁出行数据OD
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022263 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved