从同一文件夹中的多个文件创建一个csv文件

2024-05-19 13:32:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有数千个csv文件名,如下file_x_x.csv,其中x是一个介于1和10000之间的数字,在同一个文件夹中。 每个文件包括一个标题和一行数据:

文件_1_1.csv

Name Surname Age Address
Michael O'Donnel 22 George St.

文件_2_2.csv

Name Surname Age Address
Mary Jane 34 Camden St.

等等

我希望创建一个包含所有这些行的文件:

最终文件.csv

Name Surname Age Address
Michael O'Donnel 22 George St.
Mary Jane 34 Camden St.

我的做法:

import pandas as pd
import glob

path = # add path
all_files = glob.glob(path + ".csv") # look for all the csv files in that folder. Probably this is not the right code for looking at them

file_list = []

for filename in all_files:
    df = pd.read_csv(filename)
    file_list(df)

我不知道如何在最后创建一个唯一的文件。你能看一下上面的代码,告诉我如何得到想要的输出,如果我错过了什么


Tags: 文件csvpathnameforageaddressfiles
2条回答

你不需要在这里做任何复杂的事情。你知道标题行,你知道你想要的是除了标题之外的所有东西。只需打开文件,跳过第一行,然后写入。这比内存中大量数据帧的内存消耗效率要高得多

import glob

with open("final_file.csv", "w") as outfile:
    for count, filename in enumerate(glob.glob(path + ".csv")):
        with open(filename) as infile:
            header = next(infile)
            if count == 0:
                outfile.write(header)
            line = next(infile)
            if not line.startswith("\n"):
                line = line + "\n"
            outfile.write(line)

我建议使用pd.concat将数据帧组合成一个大数据帧,如果愿意,可以将其保存到另一个文件中

在连接数据帧之前,您可能必须修改对pd.read_csv的调用,以确保正确处理数据。如果问题中的示例数据与CSV文件的内容逐字匹配,则代码段如下所示:

import pandas as pd
import glob

path = "/my_path" # set this to the folder containing CSVs
names = glob.glob(path + "*.csv") # get names of all CSV files under path

# If your CSV files use commas to split fields, then the sep 
# argument can be ommitted or set to ","
file_list = pd.concat([pd.read_csv(filename, sep=" ") for filename in names])

#save the DataFrame to a file
file_list.to_csv("combined_data.csv")

请注意,组合索引中的每一行仍将基于其源文件中的行号编制索引,从而创建重复的行索引。要更改此设置,请调用pd.DataFrame.reset_index()

file_list = file_list.reset_index(drop=True)

相关问题 更多 >