如何在不插入我需要的文件的情况下合并多个Excel CSV文件

时间:2019-10-07 14:32:22

标签: python pandas operating-system

现在,我试图创建一个将多个csv文件合并到一个csv文件中的代码,因为这没有问题,并且可以正常工作。...

这是我的代码

import pandas as pd
import os



mydir = (os.getcwd()).replace('\\', '/') + '/'



# Read all three files into pandas dataframes
f1 = pd.read_csv(r'' + mydir + "BCACACBBABDBBBnir.csv", encoding='latin-1')
f2 = pd.read_csv(r'' + mydir + "BCACACBBABDBBCnir.csv", encoding='latin-1')
f3 = pd.read_csv(r'' + mydir + "BCACACBBABDBBDnir.csv", encoding='latin-1')
f4 = pd.read_csv(r'' + mydir + "BCACACBBABDBDAnir.csv", encoding='latin-1')
f5 = pd.read_csv(r'' + mydir + "BCACACBBABDBDBnir.csv", encoding='latin-1')
........
f107 = pd.read_csv(r'' + mydir + "BCACACBDBDBAABnir.csv", encoding='latin-1')
# Create a list of the files in order you want them appended
all_df_list = [f1, f2, f3, f4, ....., f107]

# Merge all the dataframes in all_df_list
# Pandas will automatically append based on similar column names
appended_df = pd.concat(all_df_list)

# Write the appended dataframe to an excel file
# Add index=False parameter to not include row numbers
appended_df.to_csv("AllBefore.csv", index=False)

因此,现在我需要插入要合并的所有csv文件,因为我有数百个csv文件,所以我只想创建一个读取所有excel {将{1}}个文件放在某个for loopcsv中,然后将它们合并为一个directory文件。

那么,有可能这样做吗?

1 个答案:

答案 0 :(得分:1)

猜测这将起作用

basepath = <basepath>
files = list(filter(lambda x: '.csv' in x, os.listdir(basepath)))
alldf = pd.DataFrame()
for f in files:
    df= pd.read_csv(f"{basepath}/{f}",encoding='latin-1')
    alldf = pd.concat([alldf,df])

alldf.to_csv("final.csv")