本文介绍了比较两个Dataframe列以检查它们在python中是否具有相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据框,

new1.
      Name       city
 0    sri won    chn
 1    pechi won  pune
 2    Ram won    mum
 0    pec won    kerala

new3
    req
0   pec
1   mut

我尝试过,

mask=new1.Name.str.contains("|".join(new3.req.values.tolist()))
new1[mask]

我正在获取,

 new1[mask]
      Name       city
 1  pechi won    pune
 0  pec won      kerala

由于 pechi包含 pec,因此采用了该值。但是我希望值之间的精确匹配不是包含

As "pechi" contains "pec", it took this valu. but I want the exact match between the values not "contains"

我想要的输出是

 new1[mask]
      Name       city
 0  pec won      kerala


推荐答案

您需要 \b 意思是单词边界:

You need \b that means "word boundary":

a = r'\b(' + "|".join(new3.req.values.tolist()) + r')\b'
print (a)
\b(pec|mut)\b

mask=new1.Name.str.contains(a)
df = new1[mask]
print (df)
      Name    city
0  pec won  kerala

这篇关于比较两个Dataframe列以检查它们在python中是否具有相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 16:48