1

I have three dataframes with index as date and a price column. I want to merge three dataframes into one and merge price column. I tried merge right as suggested by this post Python: pandas merge multiple dataframes but the result is same as concatenation with axis =1.

chained_price = reduce(lambda left, right: pd.merge(left, right, left_index=True, right_index = True,
                                                    how='right'), product_list)

product_list is a list with three df.

             price
date                
2013-09-10  1.000000
2013-09-11  1.011578
2013-09-12 -1.006452

            price
date               
2013-09-11  3.011578
2013-09-12  5.006452
2013-09-13  6.000544

            price
date               
2013-09-12  8.011578
2013-09-13  7.006452
2013-09-14  10.000544

expected result:

            price
date
2013-09-10  1.000000
2013-09-11  3.011578
2013-09-12  8.011578
2013-09-13  7.006452
2013-09-14  10.000544

Any advice?

sdg
  • 701
  • 1
  • 5
  • 12

1 Answers1

4

You need concat with drop duplicates i.e

df4 = pd.concat(product_list).drop_duplicates('date',keep='last')
         date      price
0  2013-09-10   1.000000
0  2013-09-11   3.011578
0  2013-09-12   8.011578
1  2013-09-13   7.006452
2  2013-09-14  10.000544

If date is index then

df4 = pd.concat(product_list)
df4[~df4.index.duplicated(keep='last')]
Bharath M Shetty
  • 30,075
  • 6
  • 57
  • 108