将多个.csv文件与来自不同目录路径的python组合在一起

时间:2016-06-14 19:33:14

标签: python pandas

我正在尝试使用pandas中的数据框将多个.csv文件合并到一个.csv文件中。关于这一点的棘手部分是,我需要从多天获取多个文件。如果这没有意义,请告诉我。就目前而言,我无法弄清楚如何遍历目录。你能提供一些帮助吗?

import csv
import pandas as pd
import datetime as dt
import glob, os


startDate = 20160613
endDate = 20160614

dateRange = endDate - startDate
dateRange = dateRange + 1
todaysDateFilePath = startDate


for x in xrange(dateRange):
    print startDate
    startDate = startDate + 1
    filePath = os.path.join(r"\\export\path", startDate, "preprocessed")
    os.chdir(filePath)
    interesting_files = glob.glob("trade" + "*.csv")

print interesting_files

df_list = []
for filename in sorted(interesting_files):
    df_list.append(pd.read_csv(filename))
    full_df = pd.concat(df_list)




saveFilepath = r"U:\Chris\Test_Daily_Fails"
fileList = []


full_df.to_csv(saveFilepath + '\\Files_For_IN' + "_0613_" + ".csv", index = False)

1 个答案:

答案 0 :(得分:0)

IIUC您可以创建list all_files并在循环中将glob的输出附加到all_files

all_files = []
for x in xrange(dateRange):
    print startDate
    startDate = startDate + 1
    filePath = os.path.join(r"\\export\path", startDate, "preprocessed")
    os.chdir(filePath)
    all_files = all_files + glob.glob("trade" + "*.csv")

print interesting_files

此外,您需要先将所有值附加到df_list,然后只追加concat(我为concat缩进代码):

df_list = []
for filename in sorted(interesting_files):
    df_list.append(pd.read_csv(filename))

full_df = pd.concat(df_list)