Thursday, August 30, 2018

get the common index of two dataframe

idx = df1.index.intersection(df2.index)
print (idx)
Index(['28/11/2000', '29/11/2000', '30/11/2000'], dtype='object')
Alternative solution with numpy.intersect1d:
idx = np.intersect1d(df1.index, df2.index)
print (idx)
['28/11/2000' '29/11/2000' '30/11/2000']

df1 = df1.loc[idx]
print (df1)
            values 1
28/11/2000 -0.055276
29/11/2000  0.027427
30/11/2000  0.066009

df2 = df1.loc[idx]
print (df2)
            values 1
28/11/2000 -0.055276
29/11/2000  0.027427
30/11/2000  0.066009