Home > Backend Development > Python Tutorial > 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?

Patricia Arquette
Release: 2024-10-29 04:34:29
Original
1027 people have browsed it

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:

<code class="python">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')</code>
Copy after login

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.

The above is the detailed content of How to Combine DataFrames Generated in a For Loop into a Single DataFrame?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template