Searching for many different names in a Panda frame Consider the following example code:
c1 = [&quot;Mary herself&quot;,&quot;Mary is mine&quot;,&quot;John himself&quot;,&quot;John is mine&quot;,&quot;Alan himself&quot;,&quot;Alan is mine&quot;]
s1 = pd.Series(c1)
c2 = [1,2,3,4,5,6]
s2 = pd.Series(c2)

dictionary = {&quot;A&quot;:s1,&quot;B&quot;:s2}

df = pd.DataFrame(dictionary)
print(df)
for i in range(6):
  name = df[&quot;A&quot;].iloc[i]
  if &quot;Mary&quot; in name:
    print(name)
  if &quot;Alan&quot; in name:
    print(name)
  else:
    print(&quot;no&quot;)

I want to filter strings that include &quot;Mary&quot; or &quot;Alan&quot;. My solution works well for only two cases, but I wonder if there is a more suitable solution for a lot more cases. For example, if I want to filter &quot;John&quot; and &quot;Brown&quot; too, I would already need four if statements.
How can I improve my code so I wouldn't have to add a new if statement for each name I want to filter?
Your code follows this pseudo-code pattern:
if any of these strings are in name:
    do x

The simplest way to express that in Python is to invert the condition:
if name in any of these strings:
    do x

Or as actual Python:
if name in [&quot;Alan&quot;, &quot;Mary&quot;]:
    print(name)