Home > Software engineering >  Checking to see if column has part of a string - if it does, I want to return a value to a new colum
Checking to see if column has part of a string - if it does, I want to return a value to a new colum

Time:08-17

I am currently writing a function that takes in my dataframe and checks if the campaign column contains the word "Bathroom" in a row, and if it does it creates a new column called 'Product' with the string "Bathroom" inside of it. I want Bathroom to be returned to Product even if the strings inside the campaign column aren't exactly bathroom. For example, they could be 'Bathrooms', 'Bathrooms - Des Moines', 'Bathroom remodeling' etc.

Here is what I currently have but I keep receiving an attribute error "'str' object has no attribute 'str'"

def product(x):
   bathroom = x['Campaign'].str.contains('Bathroom')
   if bathroom == True:
      return 'Bathroom'

df['Product'] = df.apply(product, axis = 1)

I can't seem to find the issue!

CodePudding user response:

Use boolean indexing:

df.loc[df['Campaign'].str.contains('Bathroom'), 'Product'] = 'Bathroom'

CodePudding user response:

What that error means is that within the function x['Campaign'] is a straight-up Python string. Not a Pandas Series. You are attempting to use the string accessor - .str - which works on a Series but not on a Python string. What you need to do is use the Python in operator.

def product(x):
   if 'Bathroom' in x['Campaign']:
      return 'Bathroom'

df['Product'] = df.apply(product, axis = 1)

With all that said @mozway has a better vectorized answer.

  • Related