Appending pandas dataframes generated in a for loop


Python Problem Overview

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.

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


Python Solutions

Solution 1 - Python

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

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

Solution 2 - Python

you can try this.

for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)

I hope it can help.

Solution 3 - Python

DataFrame.append() and Series.append() have been deprecated and will be removed in a future version. Use pandas.concat() instead (GH35407).


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEl ConfusoView Question on Stackoverflow
Solution 1 - PythonbiobirdmanView Answer on Stackoverflow
Solution 2 - Pythonye jiaweiView Answer on Stackoverflow
Solution 3 - PythonBO-CHRISTOPHER REDFEARNView Answer on Stackoverflow