I have 2 dataframes:
ID LIST_VALUES
1 [a,b,c]
2 [a,n,t]
3 [x]
4 [h,h]
VALUE MAPPING
a alpha
b bravo
c charlie
n november
h hotel
t tango
x xray
I need to add a new column to the first dataframe that shows the VALUES from the second dataframe based on what is in the LIST_VALUES list. If a value is duplicated in LIST_VALUES, only show it once in the output. So:
ID LIST_VALUES new_col
1 [a,b,c] alpha,bravo,charlie
2 [a,n,t] alpha,november,tango
3 [x] xray
4 [h,h] hotel
I have tried to merge pd.merge
but I keep getting stuck as I cannot merge with elements in a list.
df_new = df1.merge(df2, how='left', left_on='LIST_VALUES', right_on='VALUES')
This will only work where a LIST_VALUE has only 1 element so in this example ID 3. I need it to work where there are multiple values in a list.