I have two pandas DataFrames, where the first DataFrame has two columns: "a" and "id" and the second DataFrame has two columns: "id" and "color_value". I would like to compare the ids between the two DataFrames and if there is a match, add a column to the first DataFrame with the correct color_value. In essence, I am using the second DataFrame as a lookup table to find the right ID and match it to its corresponding color value.
See below.
df_1 = pd.DataFrame({'a': [1, 2, 3, 4, 5], 'id': ['001', '002', '001', '004', '003']})
df_2 = pd.DataFrame({'id': ['001', '002', '003', '004'], 'color_value': ['blue', 'red', 'yellow', 'orange']})
so df1:
a id
1 001
2 002
3 001
4 004
5 003
df2:
id color_value
001 blue
002 red
003 yellow
004 orange
I need a way to match up the IDs from df1 and df2 and add the color_value column to df1, so the result is:
df1:
a id color_value
1 001 blue
2 002 red
3 001 blue
4 004 orange
5 003 yellow