python_pandas_도로 행정구역 등급의 데이터를 그룹으로 나누어 화합시키다

# -*- coding: utf-8
import pandas as pd
'''
population|gender|age|district_O|street_O|district_D|street_D
01|10|1| | | | 
'''
def get_bus_station():
    file = 'gender_age.txt'
    Street_OD = pd.read_csv(od_src + file,encoding='gbk',usecols=[0,1,2,3,5],sep='|',names=['gender','age','od','district_O','district_D'])
    df3 = Street_OD.groupby(['gender','age','district_O','district_D']).agg({'od':sum})
    df3.name = 'od_count'
    df3.to_csv(od_src + 'od_district_gender_age_201911.csv', index=True,header=True)

    Street_OD = pd.read_csv(od_src + file,encoding='gbk',usecols=[0,1,2,4,6],sep='|',names=['gender','age','od','street_O','street_D'])
    df3 = Street_OD.groupby(['gender','age','street_O','street_D']).agg({'od':sum})
    df3.name = 'od_count'
    df3.to_csv(od_src + 'od_street_gender_age_201911.csv', index=True,header=True)

    gender = pd.read_csv(od_src + 'od_district_gender_age_201911.csv')
    age = gender[gender['age']>3]
    age = age[age['age']<14]
    age.to_csv(od_src + 'od_district_age_gender_201911.csv', index=False)

    gender = pd.read_csv(od_src + 'od_street_gender_age_201911.csv')
    age = gender[gender['age']>3]
    age = age[age['age']<14]
    age.to_csv(od_src + 'od_street_age_gender_201911.csv', index=False)


#  ,01,06,1, , , , 
def get_bus_station_202004():
    file = 'flow_age_202004.csv'
    Street_OD = pd.read_csv(od_src + file,usecols=[1,2,3,4,6],sep=',',names=['gender','age','od','district_O','district_D'])
    df3 = Street_OD.groupby(['gender','age','district_O','district_D']).agg({'od':sum})
    df3.name = 'od_count'
    df3.to_csv(od_src + 'od_district_gender_age_202004.csv', index=True,header=True)

    Street_OD = pd.read_csv(od_src + file,usecols=[1,2,3,5,7],sep=',',names=['gender','age','od','street_O','street_D'])
    df3 = Street_OD.groupby(['gender','age','street_O','street_D']).agg({'od':sum})
    df3.name = 'od_count'
    df3.to_csv(od_src + 'od_street_gender_age_202004.csv', index=True,header=True)

    gender = pd.read_csv(od_src + 'od_district_gender_age_201911.csv')
    age = gender[gender['age']>3]
    age = age[age['age']<14]
    age.to_csv(od_src + 'od_district_age_gender_202004.csv', index=False)

    gender = pd.read_csv(od_src + 'od_street_gender_age_201911.csv')
    age = gender[gender['age']>3]
    age = age[age['age']<14]
    age.to_csv(od_src + 'od_street_age_gender_202004.csv', index=False)

if __name__ == '__main__':
    od_src = r'D:\201911\\'
    get_bus_station_202004()

데이터가 필요하거나 데이터 처리를 도와주신다면 제 얘기를 좀 해주세요.

좋은 웹페이지 즐겨찾기