简体   繁体   English

如何组合 Python 中多个文件夹中的多个 CSV 文件?

[英]how to combine multiple CSV files from multiple folders in Python?

I have several CSV files each represents data for a day with no header.我有几个 CSV 文件,每个文件代表一天的数据,没有 header。 more like month-1/day-1.csv... day-30.csv - month-2/day-1.csv..?更像month-1/day-1.csv...day-30.csv - month-2/day-1.csv..? etc how can I combine all of these CSV files into one big CSV file that contains all of them?等我如何将所有这些 CSV 文件合并到一个包含所有这些文件的大 CSV 文件中?

Hi quant and welcome to SO!嗨,量化,欢迎来到 SO!

You can use following code to do this:您可以使用以下代码来执行此操作:

import os
import glob
import pandas as pd

path = '/your_directory_containing the files'
os.chdir(path)

all_filenames = [i for i in glob.glob('*.{}'.format('csv'))]

combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig')

Please note, that this code will combine all .csv -files in the specified directory.请注意,此代码将合并指定目录中的所有.csv文件。

I hope the code works for you:)我希望代码对你有用:)

This assumes that your input is in a in_data folder and the output goes into a folder called out_data and both folders are in the directory of your notebook.这假设您的输入位于in_data文件夹中,并且 output 进入名为out_data的文件夹中,并且这两个文件夹都在您的笔记本目录中。

import pandas as pd
import glob

dfs = pd.concat([pd.read_csv(f, header=None) for f in glob.glob("./in_data/month*/day*")])
dfs.to_csv("./out_data/df_combined.csv", index=False)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM