[英]Checking to see if column has part of a string - if it does, I want to return a value to a new column
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.我目前正在编写一个 function,它接收我的 dataframe 并检查活动列是否连续包含单词“Bathroom”,如果包含,它会创建一个名为“Product”的新列,其中包含字符串“Bathroom”。 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'"这是我目前拥有的,但我一直收到属性错误“'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!我似乎找不到问题!
Use boolean indexing:使用 boolean 分度:
df.loc[df['Campaign'].str.contains('Bathroom'), 'Product'] = 'Bathroom'
What that error means is that within the function x['Campaign']
is a straight-up Python string.该错误意味着在 function
x['Campaign']
中是一个直截了当的 Python 字符串。 Not a Pandas Series.不是 Pandas 系列。 You are attempting to use the string accessor -
.str
- which works on a Series but not on a Python string.您正在尝试使用字符串访问器 -
.str
- 它适用于系列,但不适用于 Python 字符串。 What you need to do is use the Python in
operator.您需要做的是
in
运算符中使用 Python。
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.尽管如此,@mozway 有一个更好的矢量化答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.