测试确切的字符串是否出现在Pandas系列中 [英] Test if exact string appears in a Pandas Series
问题描述
在Pandas df ['Column'](即pandas系列)中
In a Pandas df['Column'] (i.e. a pandas Series)
如果我使用
df['company_name'].str.contains('ABC').any()
如果条目为"ABC",我将得到"True"
I will get 'True' if an entry is "ABC"
但是,如果系列中的其他条目是"ABC PTY LTD",它也会返回(假阳性)"True"
But it will also return a (false positive) "True" if some other entry in the Series is "ABC PTY LTD"
我只想匹配条目完全为"ABC"的
I only want to match if there is an entry that is exactly "ABC"
我检查了大约50个类似的问题,但没有一个回答.
I've checked about 50 similar questions but none answer this one.
我尝试过正则表达式
rec_df['recruiters'].str.match( r'^ABC$').any()
它可以工作,但是问题是我想将"ABC"部分作为变量传递到正则表达式中,而我不知道该怎么做.
It works but the problem is I want to pass the 'ABC' part into the regex as a variable and I can't work out how.
对想要学习的NooB有任何帮助吗?
Any help for a NooB who trying to learn please?
任何与记录完全匹配"ABC",而不是更长的字符串(例如"ABC Pty Ltd")而不是子字符串(例如"AB")的解决方案都是可行的
Any solution that would match a record with exactly 'ABC' and not a longer string like 'ABC Pty Ltd' and not a substring like 'AB" would be idea
推荐答案
您可以
df['company_name'].eq('ABC').any() #(df['company_name']=='ABC').any()
这篇关于测试确切的字符串是否出现在Pandas系列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!