问题
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