I have two Pandas' data frames
df1
items view
0 A|B|C 02-10-2015
1 D|E 02-15-2015
df2
item num val
0 A 1 10
1 B 3 2
2 C 8 9
3 D 9 13
4 E 2 22
I want to mere these frames to get
df
view num1 val1 num2 val2 num3 val3
0 02-10-2015 1 10 3 2 8 9
1 02-15-2015 9 13 2 22 na na
My current approach is to split df1.items using
df3 = pd.DataFrame(df1['items'].str.split('|').tolist())
which results in
0 1 2
0 A B C
1 D E None
and finally merges each individual column and concatenates them with the original df1
x = pd.merge(df3[[0]], df2, how='left', on='item')
y = pd.merge(df3[[1]], df2, how='left', on='item')
z = pd.merge(df3[[2]], df2, how='left', on='item')
pd.concat([df1, x.ix[:,1:],y.ix[:,1:],z.ix[:,1:]], axis=1)
The code works but it doesn't seem right to me, I would be happy if anyone can point out a proper way to achieve the same results.
Thank you in advance!