有两个 Pandas 数据集
csv1 = pandas.read_csv('test1')
csv2 = pandas.read_csv('test2')
如何显示 csv1
中 str1
字段不是 csv2
任何行的子字符串的所有行str2
字段?
注意:我尝试使用 contains
但似乎它的参数pat应该是一个字符串而不是一列字符串。
示例:
#csv1
id str1
1 abc
2 def
3 ghi
4 xyz
#csv2
data1 str2
69236 pghiww
9623 habcrv
6152 de
那么输出应该是:
id str1
2 def
4 xyz
事实上,第 2 行和第 4 行的 str1
字段不包含在 csv2
的 str2
的任何行中。
最佳答案
这个问题的挑战不仅是检测是否存在匹配,还要找出匹配的内容,并进行相应的过滤。在推导式中使用 str.contains
的一个选项:
csv1 = csv1.iloc[[~csv2.str2.str.contains(x).any() for x in csv1.str1]]
print(csv1)
id str1
1 2 def
3 4 xyz
关于python - 过滤掉另一列字符串的某一行中包含字符串字段的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46858780/