I have two dataframes in python (here only the df.head(3)):
df1
Column1 | Column2 | Column3 | Column4 | Column5 |
---|---|---|---|---|
1a | 123 | RJ | 12.1 | test1 |
2a | 432 | MT | 23.2 | test3 |
3a | 234 | DR | 34.3 | test5 |
df2
Column1 | Column3 | Column6 |
---|---|---|
1a | RJ | 695 |
2a | MT | 568 |
3a | DR | 232 |
And I have created a list grouping each dataframe by Column1 having one table(lots of rows) for each value of Column1:
list_df1 =[]
grouped = df1.groupby('Column1')
for name, group in grouped:
if name == 0:
group = group.merge(df1,how='left')
elif name == 1:
group = group.merge(df1,how='left')
list_df1.append(group)
pd.concat(list_df1)
Made the same for df2:
list_df2 =[]
grouped = df2.groupby('Column1')
for name, group in grouped:
if name == 0:
group = group.merge(df2,how='left')
elif name == 1:
group = group.merge(df2,how='left')
list_df2.append(group)
pd.concat(list_df2)
And what I need is to join these two lists by Column1
The result of this join should be something like this:
final_list = ([list_df1.Column1(1a) , list_df2.Column1(1a)] ,
[list_df1.Column1(2a) , list_df2.Column1(2a)] ,
[list_df1.Column1(3a) , list_df2.Column1(3a)] )
The end of this is that for each value of Column1 I'll have one CSV file (separated by pipe "|") that appends the grouped dataframes list_df1 and list_df2.
CSV 1a
1a|123|RJ|12.1|test1
1a|XXX|YY|000|testx
...
1a|RJ|695
1a|XX|000
...
CSV 2a
2a|432|MT|23.2|test3
2a|XXX|YY|000|testx
....
2a|MT|568
2a|XX|000
...
CSV 3a
3a|234|DR|34.3|test5
3a|XXX|YY|000|testx
...
3a|DR|232
3a|XX|000
...
Any Ideas? Thanks in advance.