I have two dataframes which I need to merge/lookup values:
df1
| Key | Value |
|---|---|
| 123 | |
| 456 | |
| 789 | |
| 101 | 0.3 |
df2
| Key | Value |
|---|---|
| 123 | 0.1 |
| 456 | 0.2 |
key = distinct value. I need to look up df2 based on the Key column.
Expected result
| Key | Value |
|---|---|
| 123 | 0.1 |
| 456 | 0.2 |
| 789 | |
| 101 | 0.3 |
I have tried following:
merged_df = pd.merge(
left = df1,
right = df2,
on = 'Key',
how = 'outer',
)
This creates an additional column 'Value_y' in merged df, while I need to get the data in the original df1 'Value' column.
How could I tweak the code, or perhaps there is another function for this task? Also currently 'Key' value is 3 combined values (product code and two dates), perhaps it's possible to merge on several columns, instead of creating an additional 'Key' column for merging?