How to get the common index of two pandas dataframes?

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

    Have you tried something like

    df1 = df1.loc[[x for x in df1.index if x in df2.index]]
    df2 = df2.loc[[x for x in df2.index if x in df1.index]]
    

提交回复
热议问题