How to get the common index of two pandas dataframes?

前端 未结 8 1272
野趣味
野趣味 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 04:04

    The index object has some set-like properties so you simply can take the intersection as follows:

    df1 = df1.reindex[ df1.index & df2.index ]
    

    This retains the order of the first dataframe in the intersection, df.

提交回复
热议问题