I've seen may questions on this topic but most are the opposite of mine. I have a list of strings (column of a data frame) and a list of sub strings. I want to compare each string to the list of sub strings If it contains a sub string then return that sub-string else print 'no match'.
subs = [cat, dog, mouse]
df
Name Number SubMatch
dogfood 1 dog
catfood 3 cat
dogfood 2 dog
mousehouse 1 mouse
birdseed 1 no match
my current output looks like this though:
Name Number SubMatch
dogfood 1 dog
catfood 3 dog
dogfood 2 dog
mousehouse 1 dog
birdseed 1 dog
I suspect my code is just returning the first thing in the series, how do I change that to the correct thing in the series? Here is the Function:
def matchy(col, subs):
for name in col:
for s in subs:
if any(s in name for s in subs):
return s
else:
return 'No Match'
any(s in name for s in subs)loop in line 4 as you are already looping over the list of subs in line 3.