I am trying to figure out an efficient way to map df2
to df1
. What makes this a bit trickier, is the key can sometimes be a tuple of 2+ keys
.
df1 = {'Index':[1,2,3,4,5,6,7,8],'key':[a,a,b,(a,c),c,(a,b,c),b,a]}
df2 = {'Index':[a,b,c],'Val1':[1,2,3],'Val2':[.1,.2,.3],'Val3':[10,20,30],'Val4':[1,2,3],'Val5':[2,4,6]}
Basically, I am trying to map df2
(via the Index) to the key in df1
. The end result would have columns = [key, Val1, Val2, Val3, Val4, Val5]
, and in the result that the type(key) == tuple
, I would like a tuple of the 2+ matched Vals.
Any help is appreciated! Thanks!
source https://stackoverflow.com/questions/73513856/pandas-map-df-with-multiple-columns-to-another
Comments
Post a Comment