如何遍历 csv 文件列表并将具有通用文件名的文件编译为单个 csv 作为多列

How to iterate over a list of csv files and compile files with common filenames into a single csv as multiple columns

我目前正在遍历 csv 文件列表,并希望将具有通用文件名字符串的 csv 文件合并到一个 csv 文件中,将新 csv 文件中的数据合并为一组两个新列。我在最后一部分遇到了麻烦,因为追加命令将数据作为行添加到 csv 的底部。我试过 pd.concat,但一定是哪里出错了。任何帮助将不胜感激。

**注意代码正在使用 Python 2 - 只是为了与我正在使用的软件兼容 - Python 3 解决方案欢迎翻译。

这是我目前正在使用的代码:

rb_headers = ["OID_RB", "Id_RB", "ORIG_FID_RB", "POINT_X_RB", "POINT_Y_RB"]
for i in coords:
    if fnmatch.fnmatch(i, '*RB_bank_xycoords.csv'):
        df = pd.read_csv(i, header=0, names=rb_headers)
        df2 = df[::-1]

        #Export the inverted RB csv file as a new csv to the original folder overwriting the original
        df2.to_csv(bankcoords+i, index=False)

    
#Iterate through csvs to combine those with similar key strings in their filenames and merge them into a single csv
files_of_interest = {}
forconc = []

for filename in coords:
    if filename[-4:] == '.csv':
        key = filename[:39]
        files_of_interest.setdefault(key, [])
        files_of_interest[key].append(filename)

for key in files_of_interest:
    buff_df = pd.DataFrame()
    for filename in files_of_interest[key]:
        buff_df = buff_df.append(pd.read_csv(filename))

    files_of_interest[key]=buff_df

    redundant_headers = ["OID", "Id", "ORIG_FID", "OID_RB", "Id_RB", "ORIG_FID_RB"]

    outdf = buff_df.drop(redundant_headers, axis=1)

如果您只想合并到一个文件中:

paths_list=['path1', 'path2',...] 
dfs = [pd.read_csv(f, header=None, sep=";") for f in paths_list]
dfs=pd.concat(dfs,ignore_index=True)
dfs.to_csv(...)