检查数据集的行是否也在另一个数据集中,并提取索引

时间:2019-05-24 11:52:14

标签: python pandas

在我的问题中,我有两个要比较的数据集。这是2个数据框:





nodeid res_time_start_s res_time_end_s
195 291 1.505362e+09 1.505362e+09
390 296 1.503123e+09 1.503123e+09
391 296 1.503145e+09 1.503145e+09
413 296 1.511742e+09 1.511742e+09
414 296 1.511742e+09 1.511742e+09
471 297 1.503145e+09 1.503145e+09
472 297 1.504822e+09 1.504822e+09
484 297 1.511742e+09 1.511742e+09
549 304 1.505470e+09 1.505470e+09
552 304 1.506010e+09 1.506010e+09





      nodeid  res_time_start_s  res_time_end_s
195      291      1.505362e+09    1.505362e+09
390      296      1.503123e+09    1.503123e+09
391      296      1.503145e+09    1.503145e+09
413      296      1.511742e+09    1.511742e+09
414      296      1.511742e+09    1.511742e+09
471      297      1.503145e+09    1.503145e+09
472      297      1.504822e+09    1.504822e+09
484      297      1.511742e+09    1.511742e+09
549      304      1.505470e+09    1.505470e+09
552      304      1.506010e+09    1.506010e+09
553      304      1.506031e+09    1.506031e+09
554      304      1.506053e+09    1.506053e+09
555      304      1.506053e+09    1.506053e+09
556      304      1.506096e+09    1.506096e+09
557      304      1.506096e+09    1.506096e+09
558      304      1.506312e+09    1.506312e+09
559      304      1.506312e+09    1.506312e+09
560      304      1.506334e+09    1.506334e+09
561      304      1.506399e+09    1.506399e+09
562      304      1.506442e+09    1.506442e+09
563      304      1.506442e+09    1.506442e+09
564      304      1.506485e+09    1.506485e+09
565      304      1.506507e+09    1.506507e+09
2059     568      1.519189e+09    1.519189e+09
2082     569      1.518131e+09    1.518131e+09




我想检查第二个数据帧中是否有第一个数据帧的行,如果有,请获取第二个数据帧中该行的索引并将其附加到数组中。 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

DataFrame.mergeDataFrame.reset_index一起使用,以避免删除索引:

a = df1.merge(df2.reset_index())['index'].tolist()
print (a)
[195, 390, 391, 413, 414, 413, 414, 471, 472, 484, 549, 552]