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
str. contains() function is used to test if pattern or regex is contained within a string of a Series or Index. The function returns boolean Series or Index based on whether a given pattern or regex is contained within a string of a Series or Index.
Exact match (equality comparison): == , != As with numbers, the == operator determines if two strings are equal. If they are equal, True is returned; if they are not, False is returned. It is case-sensitive, and the same applies to comparisons by other operators and methods.
Using “contains” to Find a Substring in a Pandas DataFrame The contains method in Pandas allows you to search a column for a specific substring. The contains method returns boolean values for the Series with True for if the original Series value contains the substring and False if not.
iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.
You can do
df['company_name'].eq('ABC').any() #(df['company_name']=='ABC').any()
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With