nrs90
nrs90

Reputation: 168

Joining Pandas dataframes in Python with the same index multiple times

I have two dataframes:

>>>import import pandas as pd
>>>df1:

                 DD_PRICE
DATE                         
2013-09-19 20:55:00  3.516
2013-09-19 20:55:00  3.516
2013-09-19 20:55:00  3.516
2013-09-19 20:55:00 -4.476

>>>df2:
                 CAPI_PRICE
DATE                         
2013-09-19 20:55:00  3.516
2013-09-19 20:55:00  3.516
2013-09-19 20:55:00  3.516
2013-09-19 20:55:00 -4.476

Now when I run:

 joint_df = df1.join((df2), how='outer')

joint_df looks like this:

                  DD_PRICE  CAPI_PRICE
DATE                                   
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516 -4.476
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516 -4.476
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516 -4.476
2013-09-19 20:55:00 -4.476  3.516
2013-09-19 20:55:00 -4.476  3.516
2013-09-19 20:55:00 -4.476  3.516
2013-09-19 20:55:00 -4.476 -4.476

This isnt what I want...

Ideally I want joint_df to look like this:

                  DD_PRICE  CAPI_PRICE
DATE                                   
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00  3.516  3.516
2013-09-19 20:55:00 -4.476 -4.476

How do I get this done? Tried different combos of merge and join parameters but I can't get it to work!

Upvotes: 0

Views: 3941

Answers (2)

Ioannis Nasios
Ioannis Nasios

Reputation: 8527

You could try to just add a new column on first data frame by copying it from second data frame like this:

df1['CAPI_PRICE']=df2['CAPI_PRICE']

Upvotes: 0

jezrael
jezrael

Reputation: 863056

It seems you can use concat only - by default there is outer join:

df = pd.concat([df1, df2], axis=1)
print (df)
                     DD_PRICE  CAPI_PRICE
DATE                                     
2013-09-19 20:55:00     3.516       3.516
2013-09-19 20:55:00     3.516       3.516
2013-09-19 20:55:00     3.516       3.516
2013-09-19 20:55:00    -4.476      -4.476

Upvotes: 1

Related Questions