Aless
Aless

Reputation: 60

Join dataframes from loop output using Pandas

I want to create one dataframe from dataframes that are output from a for loop. Each iteration of the loop produces the variable called result.

 result = pd.concat([df1, df2], axis=1)

I want to create one dataframe that is the combination (adjacent to one another) of each result dataframe.

before the loop I created a variable combined_results

combined_results = pd.DataFrame()

And at the end of the loop tried to store each result in that variable, using append, concat etc but cant get it to work.

    combined_results = combined_results.append(result)

This appends vertically not horizontally and tried concat as per result, but no luck

Upvotes: 0

Views: 671

Answers (2)

mcskinner
mcskinner

Reputation: 2748

You are already on the right track with the pd.concat([...], axis=1) pattern you use inside the loop. The trick is to save all your partial DataFrames into a list, and then save concatenation until the end.

I use a pattern like this:

combined_results = []
for i in range(5):
    df1 = pd.DataFrame({f'x{i}': ['a', 'b', 'c']})
    df2 = pd.DataFrame({f'y{i}': [i, i*2, i*3]})
    result = pd.concat([df1, df2], axis=1)
    combined_results.append(result)

# Use axis=1 as before to join horizontally instead of vertically.
combined_results = pd.concat(combined_results, axis=1)

combined_results
#   x0  y0 x1  y1 x2  y2 x3  y3 x4  y4
# 0  a   0  a   1  a   2  a   3  a   4
# 1  b   0  b   2  b   4  b   6  b   8
# 2  c   0  c   3  c   6  c   9  c  12

Upvotes: 1

Cristian Contrera
Cristian Contrera

Reputation: 713

in your loop you can do this:

df_results = [] 

for ... in ...:
    result = pd.concat([df1, df2], axis=1)
    df_results.append(result)

df = pd.concat(df_results)

If you show us from where you get df1 and df2, maybe we can improve this

Upvotes: 0

Related Questions