简体   繁体   English

检查列是否包含字符串的一部分 - 如果有,我想将值返回到新列

[英]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.

相关问题 我想使用列表中字符串匹配的值创建一个新列 - I want to create a new column with the value that a string has matched on from a list 我想选择某些列并创建新列,如果在 pyhton 中为 true,它将返回列名? - i want to select certain column and create new column which ll return column name if true in pyhton? 如果另一列具有相同的值,则检查一列是否具有相同的值 - Checking if a column has the same values if the other column has the same value 我想检查哪个 Vin 在 Module 列中具有 abs 值 - I want to check which Vin has abs value in column Module 我想用天数不同的新日期值更新一列 - I want to update a column with a new value of date different in days 我想在 dataframe 中迭代,在另一个 dataframe 中添加值(新列) - I want to iterate in dataframe adding value( new column) in another dataframe 熊猫-将列的一部分聚合为新列中的新值 - pandas - aggregate part of column to new value in new column 数据框行和列是否包含字符串? 如果是这样,则在新列中返回该字符串 - Does data frame row and column contains string? If so, return that string in new column 如何将字符串的一部分从列复制到 pandas 的新列中 - How can I copy a part of a string from a column into a new column in pandas 假设我有一列,我想查看相同的值是否显示在 5 个索引之外 - Let's say I have a column and I want to see if the same value shows up within 5 indexes away
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM