Home > Backend Development > Python Tutorial > How to Combine Pandas DataFrames Generated in a For Loop: A Comprehensive Solution

How to Combine Pandas DataFrames Generated in a For Loop: A Comprehensive Solution

Barbara Streisand
Release: 2024-10-30 14:01:02
Original
374 people have browsed it

How to Combine Pandas DataFrames Generated in a For Loop: A Comprehensive Solution

Combining Pandas DataFrames Generated in a For Loop: A Comprehensive Solution

When it comes to data manipulation, Pandas offers a powerful set of tools for working with structured data. One common task is to combine data from multiple sources. One way to achieve this is by generating dataframes in a for loop and then appending them to create a unified dataframe.

To append dataframes generated in a for loop, you'll need to utilize a slightly different approach from the one you tried. The code you provided:

appended_data = pandas.DataFrame.append(data) # requires at least two arguments
Copy after login

requires at least two dataframes as arguments, which is not suitable for appending multiple dataframes one by one. Instead, we can employ pd.concat to merge a list of dataframes into a single, larger dataframe.

Here's an improved solution:

<code class="python">appended_data = []
for infile in glob.glob("*.xlsx"):
    data = pandas.read_excel(infile)
    # Store each dataframe in a list
    appended_data.append(data)
# Concatenate the list of dataframes into a single dataframe
appended_data = pd.concat(appended_data)
# Write the resulting dataframe to a new Excel file
appended_data.to_excel('appended.xlsx')</code>
Copy after login

In this revised code:

  1. We create an empty list appended_data to store individual dataframes.
  2. Within the loop, we read each Excel file into a dataframe and append it to this list.
  3. Using pd.concat, we merge all the dataframes in the list into a single dataframe named appended_data.
  4. Finally, we write the appended dataframe to a new Excel file named "appended.xlsx".

This approach ensures that all dataframes generated in the loop are combined into a single dataframe, providing you with a unified dataset.

The above is the detailed content of How to Combine Pandas DataFrames Generated in a For Loop: A Comprehensive Solution. 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