这不一定需要在 pandas 中完成,但如果可以在 pandas 中完成就更好了。
假设我有一个列表或一系列字符串:
['XXY8779','0060-19','McChicken','456728']
我还有另一个列表或系列,其中包含原始字符串的子字符串,如下所示:
['60-19','Chicken','8779','1124231','92871','johnson']
这将返回如下内容:
[True, True, True, False]
我正在寻找类似这样的匹配项:
^[a-zA-Z0-9.,$;]+ < matching string in other list >
换句话说,以任何字符的 1 个或多个开头但其余字符与我的其他列表中的一个字符串完全匹配的内容。
有没有人对实现此目标的最佳方式有任何想法?
谢谢!
最佳答案
使用str.contains
'|'.join(s2)
生成一个字符串,告诉 contains
使用 regex
并使用 or 逻辑。
s1 = pd.Series(['XXY8779', '0060-19', 'McChicken', '456728'])
s2 = ['60-19', 'Chicken', '8779', '1124231', '92871', 'johnson']
s1.str.contains('|'.join(s2))
0 True
1 True
2 True
3 False
dtype: bool
关于python - Pandas 在另一个系列的一个系列中找到 super 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085069/