python_将客流od数据转成csv矩阵形式

数据处理 专栏收录该内容
23 篇文章 0 订阅

本文章数据来源请参考这篇文章:
python_pandas_将街道行政区级别的数据进行分组求和

import pandas as pd
from utils.read_write import readCsv, writeCSV

'''
district_O,district_D,od
1区,2区,2919139
'''
def district():
    file = 'od_district_004.csv'
    data = readCsv(dir+file)
    fill = []
    finish = ['行政区']
    for i in range(1,len(data)):
        if i <14:
            finish.append(data[i][1])
    fill.append(finish)
    data2 = pd.read_csv(dir + file)
    groups = data2.groupby('district_O')
    for name,group in groups:
        finish = []
        finish.append(name)
        for one in group.itertuples(index=False):
            finish.append(one[2])
        fill.append(finish)
    writeCSV(fill, dir + '行政区客流矩阵' + "_" + '004' + '.csv')

if __name__ == '__main__':
    dir = r'D:\学习文件\数据库\201\\'
    streetFile = '街道169.txt'
    district()
    file = 'od_street_04.csv'
    street = readCsv(dir+streetFile)
    data = readCsv(dir+file)
    fill = []
    title = ['街道']
    for i in range(0,len(street)):
        title.append(street[i][0])
    fill.append(title)
    data2 = pd.read_csv(dir + file)
    groups = data2.groupby('street_O')
    for name,group in groups:
        finish = []
        names = []
        finish.append(name)
        for one in group.itertuples(index=False):
            d = one[1]
            names.append(d)
        combine = []
        for name in names:
            for k in range(1, len(title)):
                if title[k] == name:
                    combine.append(title.index(name))
        for m in range(1, 169):
            if m in combine:
                d_name = names[combine.index(m)]
                col_value = group[group['street_D']==d_name].iat[0,2]
                finish.append(col_value)
            else:
                finish.append(0)
        fill.append(finish)
    writeCSV(fill, dir + '街道客流矩阵' + "_" + '24' + '.csv')

其中的参考文件请移步到我的下载
我的下载

如需数据或者帮忙处理数据请私聊我.。。
如果觉得写得好请关注我哦,原创不易

  • 0
    点赞
  • 0
    评论
  • 4
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值