Skip to content
Advertisement

Get a value from dataframe with different shape based on two columns

I have two dataframes colored by approximately matching marks:

df1: enter image description here

df2: enter image description here

The “marks” are not the same in each of them, but some are close. How can I copy the “Evaluated” value from df2 to df1 based on relevant “name” and “mark”?

My code is:

JavaScript

Expected result is df3

df3: enter image description here

How can I do an approximate match and get the value of the last column? I tried to use df.loc and df.where but they didn’t work because tables are in different shapes. What I expect is similar function to Excel’s Vlookup function where approximation is True. My code changes the values at the end, which I would love to keep as it was in df1. Probably I could make a copy from what I had before, but I believe there is a more pythonic way to solve it, rather than merging the tables.

Thanks in advance!

Advertisement

Answer

You can try pandas.merge_asof

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement