How to get the common index of two pandas dataframes?

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

    I found pd.Index and set combination much faster than numpy.intersect1d as well df1.index.intersection(df2.index). Here is what I used:

    df2 = df2.loc[pd.Index(set(df1.index)&set(df2.index))]

提交回复
热议问题