0

Is there a way I can assign a not operator within a string itself when using Series.str.contains? (Not with a ~ operator.) For example:

I want cat and dog but not fox.

srStr = 'cat|dog' # input
df = pd.DataFrame({'Animals' : ['cat','dog','fox','elephant']}) # cannot change
df.Animals.str.contains(srStr) # cannot change

I want to set up srStr such that the last line of the code outputs [True,True,False,True].

I cannot use the ~ operator for inverting a temporary result because the code is already part of an architecture and I am passing srStr as an input.

timgeb
  • 76,762
  • 20
  • 123
  • 145
Dhruv Mahajan
  • 289
  • 1
  • 2
  • 13

1 Answers1

1

You could use srStr = '^(?!fox$)' (negative lookahead).

timgeb
  • 76,762
  • 20
  • 123
  • 145