Suppose I have two data frames:
a = {'col1': ['value_1'], 'col2': ['value_4']}
df_a = pd.DataFramed = pd.DataFrame(data=a)
b = {'col3': [['value_1', 'value_2']], 'col4': ['value_6']}
df_b = pd.DataFramed = pd.DataFrame(data=b)
I want to then merge the two data frames on columns col1
and col3
, if the value in col1
is in the list for col3
.
The expected result is
>>> df_merged
col1 col2 col3 col4
0 value_1 value_4 ['value_1', 'value_2'] 'value_6'
I am able to deconstruct the list, by getting the list by value:
ids = df_b.iloc[0]['col3']]
and then I can iterate over the list, and insert the list values into new columns in df_b, etc., and then I continue on by doing multiple merges, etc., etc., but this is ugly and seems very arbitrary.
Thus, I am looking for a clean and "pythonic" (read as elegant and generalized) way of doing the merge.