Compare columns in pandas and merge

Viewed 787

so I have two data frames that I would like to merge on a column called offer_codes. All of the rows have multiple offer codes in a list (I could probably convert it to a tuple) and I want to match up the offer codes with the second data frame and merge on them. One of the data frames returns a list and the other is just one value, but I would like to merge it so that it merges. The data frame comes from sales data from a website.

df = pd.DataFrame(data={'available': [False, True, True],
                        'count': [190,285,165],
                        'offer_codes': ['no_offer_code',['G545', 'G1891'],['G92182', 'G1921']]})
df2 = pd.DataFrame(data={'price':[85.00,99.00],
                         'offer_codes':['G1891', 'G1921'],
                         'after_fees':[105, 121]})

I would like to merge these but my issue is that lists are unhashable when I try to merge with tuples don't seem to match up correctly.

#first df
   available  count      offer_codes
0      False    190    no_offer_code
1       True    285    [G545, G1891]
2       True    165  [G92182, G1921]
#2nd df
   after_fees offer_codes  price
0         105       G1891   85.0
1         121       G1921   99.0
#after the merge
   after_fees  available  count offer_codes  price
0         105       True    285       G1891   85.0
1         121       True    165       G1921   99.0

I thought that putting the list into a tuple would work but it definitely didn't.

1 Answers

A little bit long ..

df.set_index(['available','count']).offer_codes.apply(pd.Series).stack().\
      to_frame('offer_codes').\
          reset_index(level['count','available']).\
            merge(df2,on='offer_codes',how='left').dropna()
Out[59]: 
   available  count offer_codes  after_fees  price
2       True    285       G1891       105.0   85.0
4       True    165       G1921       121.0   99.0
Related