"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Combine DataFrames Generated in a For Loop into a Single DataFrame?

How to Combine DataFrames Generated in a For Loop into a Single DataFrame?

Published on 2024-11-08
Browse:360

How to Combine DataFrames Generated in a For Loop into a Single DataFrame?

Appending DataFrames Generated in a For Loop

When working with numerous Excel files that need to be combined into a single DataFrame, you may encounter the challenge of appending the dataframes during iteration. This question addresses this issue, where a user attempted to append dataframes within a for loop but faced difficulties.

The provided solution utilizes the pd.concat function to effectively merge a list of dataframes into a single DataFrame. The code snippet below demonstrates this approach:

appended_data = []
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    # Store DataFrame in a list
    appended_data.append(data)
# See pd.concat documentation for more info
appended_data = pd.concat(appended_data)
# Write DataFrame to an excel sheet
appended_data.to_excel('appended.xlsx')

By iteratively reading excel files and appending their dataframes to a list, pd.concat is then used to combine all dataframes into a single entity. This final dataframe can be saved as a new Excel file using the to_excel function.

This approach allows for the accumulation of data from multiple files into a single DataFrame, providing a comprehensive view of the combined data for further analysis or processing.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3