Search code examples
pythondataframemergeconcatenation

Merge data frames based on date


I want to merge 2 data frames.

The first data frame looks like:

Date    Value
1968-04-01  38.0
1968-04-02  37.6
1968-04-03  37.7
1968-04-04  36.7
1968-04-05  37.2
1968-04-08  37.0
1968-04-09  37.25
1968-04-10  37.6

The second data frame looks like:

1991-06-21  4.44
1991-06-22  4.39
1991-06-24  4.39
1991-06-25  4.37
1991-06-26  4.41
1991-06-27  4.36

Both data sets go up to present day.

How do I create a new dataframe that starts the data in 1991 since that is where the second data set starts? And has a column for each price.

I tried:

df_all_rows = pd.concat([df1, df2])

but this just puts one set of data below the other one.

I guess currently the data does not go up to present day. But how can I just create a new dataframe with only dates that both series have?

Here is what I have:

import numpy as np
import pandas as pd
import pickle
import quandl
from datetime import datetime

df1=quandl.get("BUNDESBANK/BBK01_WT5511", authtoken="6F92X3NEV8DdrhAc_d5_")
df2=quandl.get("PERTH/SLVR_USD_D", authtoken="6F92X3NEV8DdrhAc_d5_")
df2=df2.dropna()

T1 = pd.merge(df1, df2, on=df1.index, how='inner')

Solution

  • try:

    import pandas as pd
    merge=pd.merge(df1,df2, how='inner', left_index=True, right_index=True)
    

    you can change the way you merge with the how parameter. See for more information on joining here