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

Closest value join with pandas

I have a left table

enter image description here

and a right table

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

enter image description here

And I would like to get the final table

enter image description here

For this, I would like to use a function (merge or other more advanced verions like merge_asof or merge_ordered) in pandas

  • the match for v1 and v2 is exact
  • then the match for v3 is approximate, with the closest values.

>Solution :

Try with merge_asof

out = pd.merge_asof(df1.sort_values('v3'),
                    df2.sort_values('v3'), 
                    by = ['v1','v2'], 
                    on = 'v3', 
                    direction = 'nearest')

Out[10]: 
  v1 v2  v3    c
0  a  c  11  100
1  a  d  22  200
2  b  c  33  200
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