0

I'm new to python/pandas and I had a question of merging two dataframes together.

Consider I had

df1:

            Portfolio Value
Date                       
2016-07-01     100000
2016-07-08     100005
2016-07-15     100010
2016-07-22     100015
2016-07-29     100020
2016-08-05     100025
2016-08-12     100030
2016-08-19     100035
2016-08-26     100040
2016-09-02     100045
2016-09-09     100050
2016-09-16     100055
2016-09-23     100060
2016-09-30     100065
2016-10-07     100070
2016-10-14     100075
2016-10-21     100080

and

df2:
   Portfolio Value
0     100015
1     100030
2     100040
3     100075

I would like to result in a dataframe that keeps the values and date of df1 that also exists in df2 as shown below:

newdf:

            Portfolio Value
Date                       
2016-07-22     100015
2016-08-12     100030
2016-08-26     100040
2016-10-14     100075

I believe that this a merge, but I am not certain. If any other implementation would be better that is fine as well.

Any tips would be greatly appreciated!

Edit

an inner join was attempted as shown in the possible duplicate by the first comment but it does not keep the date index. Is there a way to get the dates with the Portfolio Values to show as well?

lee.edward01
  • 453
  • 1
  • 6
  • 16
  • @Abhishek, I apologize I did not realize that there might be a solution. However, if i do the innerjoin as shown in the link the Date index does not not remain is there a way that I can keep the Date index of `df1`? – lee.edward01 May 31 '18 at 02:43
  • https://stackoverflow.com/questions/11976503/how-to-keep-index-when-using-pandas-merge – ababuji May 31 '18 at 02:45
  • 1
    `df2.merge(df1.reset_index(), on='Portfolio Value').set_index('Date')` – AChampion May 31 '18 at 02:49

0 Answers0