-2

I made a for loop that creates a different pandas dataframe on each iteration. Something like this -> First iteration:

index Letter Value
0 A 1
1 B 2
2 C 3

Second iteration:

index Letter Value
0 C 5
1 D 3
2 E 1
3 F 2

Third iteration:

index Letter Value
0 A 2
1 F 1

I want to save each dataframe to a new one that looks like this:

index Letter Value Value Value
0 A 1 2
1 B 2
2 C 3 5
3 D 3
4 E 1
5 F 2 1

Also, new letters can appear on each iteration, so for example if 'G' appears for the first time on interation 'n', a new row would need to be created on the desired consolidated dataframe.

2
  • 2
    I believe you’ve forgotten to ask a question. Commented Apr 27, 2022 at 21:11
  • Do you have the final table and what to add in a new row? with an extra column as well? Commented Apr 27, 2022 at 21:13

1 Answer 1

0

You can make Letter the index for each dataframe, and then use pd.concat with axis=1:

dataframes = [df1, df2, df3]
new_df = pd.concat([d.set_index('Letter') for d in dataframes], axis=1)

Output:

>>> new_df
        Value  Value  Value
Letter                     
A       1.0   NaN     2.0  
B       2.0   NaN    NaN   
C       3.0    5.0   NaN   
D      NaN     3.0   NaN   
E      NaN     1.0   NaN   
F      NaN     2.0    1.0  
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.