-1

Following is one of my dataframe structure:

strike  coi  chgcoi
120     200  20
125     210  15
130     230  12
135     240   9

and the other one is:

strike  poi  chgpoi
125     210  15
130     230  12
135     240   9
140     225  12

What I want is:

strike  coi chgcoi  strike  poi  chgpoi
120     200 20      120       0    0
125     210 15      125     210   15
130     230 12      130     230   12
135     240  9      135     240    9
140       0  0      140     225   12
John
  • 2,820
  • 3
  • 30
  • 50
  • @enke - Well, I had concat'd 2 dataframes and resulting was this. I was not aware of `merge`. Logically, it should not be repeated. – John Mar 17 '22 at 17:04

2 Answers2

1

First, you need to create two dataframes using pandas

df1 = pd.Dataframe({'column_1': [val_1, val_2, ..., val_n], 'column_2':[val_1, val_2, ..., val_n]})
df2 = pd.Dataframe({'column_1': [val_1, val_2, ..., val_n], 'column_2':[val_1, val_2, ..., val_n]})

Then you can use outer join

df1.merge(df2, on='common_column_name', how='outer')
0
db1
strike  coi     chgcoi
0   120     200     20
1   125     210     15
2   130     230     12
3   135     240     9

db2
strike  poi     chgpoi
0   125     210     15
1   130     230     12
2   135     240     9
3   140     225     12


merge = db1.merge(db2,how="outer",on='strike')
merge
    strike  coi     chgcoi  poi     chgpoi
0   120     200.0   20.0    NaN     NaN
1   125     210.0   15.0    210.0   15.0
2   130     230.0   12.0    230.0   12.0
3   135     240.0   9.0     240.0   9.0
4   140     NaN     NaN     225.0   12.0

merge.fillna(0)
strike  coi     chgcoi  poi     chgpoi
0   120     200.0   20.0    0.0     0.0
1   125     210.0   15.0    210.0   15.0
2   130     230.0   12.0    230.0   12.0
3   135     240.0   9.0     240.0   9.0
4   140     0.0     0.0     225.0   12.0

This is your expected result with the only difference that 'strike' is not repeated

domiziano
  • 440
  • 3
  • 13