比较两个不同大小的 pandas 数据帧并找到具有相等值的行索引

Comparing two different sized pandas Dataframes and to find the row index with equal values

我需要一些帮助来比较两个 pandas 数据帧

我有两个数据框

第一个数据帧是

df1 =
   a   b   c   d
0  1   1   1   1
1  0   1   0   1
2  0   0   0   1
3  1   1   1   1
4  1   0   1   0
5  1   1   1   0
6  0   0   1   0
7  0   1   0   1

第二个数据帧是

df2 = 
   a   b   c   d
0  1   1   1   1
1  1   0   1   0
2  0   0   1   0

我想找到数据帧 1 (df1) 的行索引,其中整行与数据帧 2 (df2) 中的行相同。我的预期结果是

0
3
4
6

上面索引的顺序不需要按顺序,我只要dataframe 1(df1)的索引

有没有不用for循环的方法?

谢谢

汤米

您可以使用 merge

df1.merge(df2,indicator=True,how='left').loc[lambda x : x['_merge']=='both'].index
Out[459]: Int64Index([0, 3, 4, 6], dtype='int64')