Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Joining 2 dataframe based on a column

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading