safegraph数据预处理(二):将指定文件夹内的.csv文件合并

将weekly-pattern-2020-12-14-backfill中的,指定文件夹内(当前层)的xxx-partx.csv合并。

import pandas as pd
import os

file_dir = 'D:/baidu_netdisk/safegraph/weeklyPlacesPatterns/patterns_backfill/2020/12/14/21/2020/06/08'
csv_list =[]

# 将该文件夹下的所有文件名存入列表
files = os.listdir(file_dir)

for file in files:
    if file[-4:]=='.csv' and file[-9:-5]=='part':
        csv_list.append(file_dir+'/'+file)
print('csv_list:')
for csv in csv_list:
    print(csv)

# 获取列表的长度
length = len(csv_list)
split=file_dir[-10:].split('/')
new_file_loc = file_dir+'/'+split[0]+'-'+split[1]+'-'+split[2]+'-'+'weekly-patterns-'+str(length)+'in1'+'.csv'
print('new_file_loc:')
print(new_file_loc)
# 读取第一个CSV文件并包含表头,用于后续的csv文件拼接
df = pd.read_csv(csv_list[0]) 
# 读取第一个CSV文件并保存
df.to_csv(new_file_loc,index=False)

# 循环遍历列表中各个CSV文件名,并完成文件拼接
for i in range(1,length):
    df = pd.read_csv(csv_list[i])
    df.to_csv(new_file_loc,index=False, header=False, mode='a')

输出结果:

csv_list:
D:/baidu_netdisk/safegraph/weeklyPlacesPatterns/patterns_backfill/2020/12/14/21/2020/06/08/patterns-part1.csv
D:/baidu_netdisk/safegraph/weeklyPlacesPatterns/patterns_backfill/2020/12/14/21/2020/06/08/patterns-part2.csv
D:/baidu_netdisk/safegraph/weeklyPlacesPatterns/patterns_backfill/2020/12/14/21/2020/06/08/patterns-part3.csv
D:/baidu_netdisk/safegraph/weeklyPlacesPatterns/patterns_backfill/2020/12/14/21/2020/06/08/patterns-part4.csv
D:/baidu_netdisk/safegraph/weeklyPlacesPatterns/patterns_backfill/2020/12/14/21/2020/06/08/patterns-part5.csv
new_file_loc:
D:/baidu_netdisk/safegraph/weeklyPlacesPatterns/patterns_backfill/2020/12/14/21/2020/06/08/2020-06-08-weekly-patterns-5in1.csv

你可能感兴趣的:(safegraph,python)