I have a dataframe df_1 like below :
| A | B |
+----------+-----------+
|A | 120.0 |
|D | 2.50 |
|N | 1.00 |
|N | 0.50 |
|D | 1.50 |
|A | 240.0 |
+----------+-----------+
And I have another dataframe df_2 like below :
| J | K | L | M |
+-----------+----------+-----------+-----------+
| 0.50 | 4.1 | 70.0 | 55.0 |
| 0.75 | 6.7 | 80.0 | 66.0 |
| 1.00 | 8.2 | 90.0 | 95.0 |
| 1.50 | 9.6 | 120.0 | 186.0 |
| 2.50 | 10.7 | 240.0 | 188.0 |
+-----------+----------+-----------+-----------+
The goal is to create a new column called Z in the df_1 where in we lookup the values of column B (df_1) in df_2 based on the condition in column A (df_1).For example, in column Z as show below , the value of A is 186.0 which is obtained by looking up the value 120.0 (in df_1) in the columns L and M in df_2.Similarly, the values for D,N are obtained by looking in the columns J and K respectively. The final dataframe looks like below
| A | B | Z |
+----------+-----------+----------
|A | 120.0 | 186.0 |
|D | 2.50 | 10.7 |
|N | 1.00 | 8.2 |
|N | 0.50 | 4.1 |
|D | 1.50 | 9.6 |
|A | 240.0 | 188.0 |
+----------+-----------+---------+
So, how can the above be achieved in Pandas similar to how we can achieve in Excel vlookup ?
EDIT :
What if the df_2 looked like below ? Can we still get the same results as above ?
| J | K | L | M |
+-----------+----------+-----------+-----------+
| 0.50 | 4.1 | 70.0 | 55.0 |
| 0.75 | 6.7 | 80.0 | 66.0 |
| 1.00 | 8.2 | 90.0 | 95.0 |
| 1.50 | 9.6 | 120.0 | 186.0 |
| 2.50 | 10.7 | 240.0 | 188.0 |
| 3.50 | 2.2 | NaN | NaN |
+-----------+----------+-----------+-----------+
final_dataframe- Why the values in columnBis different from the original?