Test if exact string appears in a Pandas Series

假如想象 提交于 2020-02-15 08:34:11

问题


In a Pandas df['Column'] (i.e. a pandas Series)

If I use

df['company_name'].str.contains('ABC').any()

I will get 'True' if an entry is "ABC"

But it will also return a (false positive) "True" if some other entry in the Series is "ABC PTY LTD"

I only want to match if there is an entry that is exactly "ABC"

I've checked about 50 similar questions but none answer this one.

I tried a Regex

rec_df['recruiters'].str.match( r'^ABC$').any()

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.

Any help for a NooB who trying to learn please?

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


回答1:


You can do

df['company_name'].eq('ABC').any() #(df['company_name']=='ABC').any()



回答2:


Thanks to @Wen for the answer. I also worked out the Regex approach in case anyone needs it.

company_name = 'ABC'

item = r'^' + company_name + '$' 

df[‘company’].str.match(item).any()


来源:https://stackoverflow.com/questions/51728426/test-if-exact-string-appears-in-a-pandas-series

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!