Issue
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.
Solution
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
Answered By - richardec
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.