附加for循环中生成的pandas数据帧

时间:2021-11-30 04:51:12

I am accessing a series of Excel files in a for loop. I then read the data in the excel file to a pandas dataframe. I cant figure out how to append these dataframes together to then save the dataframe (now containing the data from all the files) as a new Excel file.

我在for循环中访问一系列Excel文件。然后我将excel文件中的数据读取到pandas数据帧。我无法弄清楚如何将这些数据帧附加到一起,然后将数据帧(现在包含来自所有文件的数据)保存为新的Excel文件。

Here's what I tried:

这是我尝试过的:

for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    appended_data = pandas.DataFrame.append(data) # requires at least two arguments
appended_data.to_excel("appended.xlsx")

Thanks!

2 个解决方案

#1


65  

Use pd.concat to merge a list of DataFrame into a single big DataFrame.

使用pd.concat将DataFrame列表合并到一个大的DataFrame中。

appended_data = []
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    # store DataFrame in list
    appended_data.append(data)
# see pd.concat documentation for more info
appended_data = pd.concat(appended_data, axis=1)
# write DataFrame to an excel sheet 
appended_data.to_excel('appended.xlsx')

#2


16  

you can try this.

你可以试试这个。

data_you_need=pd.DataFrame()
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    data_you_need=data_you_need.append(data,ignore_index=True)

I hope it can help.

我希望它可以提供帮助。

#1


65  

Use pd.concat to merge a list of DataFrame into a single big DataFrame.

使用pd.concat将DataFrame列表合并到一个大的DataFrame中。

appended_data = []
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    # store DataFrame in list
    appended_data.append(data)
# see pd.concat documentation for more info
appended_data = pd.concat(appended_data, axis=1)
# write DataFrame to an excel sheet 
appended_data.to_excel('appended.xlsx')

#2


16  

you can try this.

你可以试试这个。

data_you_need=pd.DataFrame()
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    data_you_need=data_you_need.append(data,ignore_index=True)

I hope it can help.

我希望它可以提供帮助。