How to get the common index of two pandas dataframes?

前端 未结 8 1420
野趣味
野趣味 2021-02-19 03:13

I have two pandas DataFrames df1 and df2 and I want to transform them in order that they keep values only for the index that are common to the 2 dataframes.

df1

8条回答
  •  不思量自难忘°
    2021-02-19 03:54

    In [352]: common = df1.index.intersection(df2.index)
    
    In [353]: df1.loc[common]
    Out[353]:
                 values1
    0
    28/11/2000 -0.055276
    29/11/2000  0.027427
    30/11/2000  0.066009
    
    In [354]: df2.loc[common]
    Out[354]:
                 values2
    0
    28/11/2000 -0.026316
    29/11/2000  0.015222
    30/11/2000 -0.024480
    

提交回复
热议问题