简体   繁体   English

使用字符串添加新列包含 python

[英]Adding new column using string contains python

I need some help, I want to add a new column from parts of a existing column.我需要一些帮助,我想从现有列的一部分中添加一个新列。 I used str.contains but it gives me this error: unsupported operand type(s) for &: 'str' and 'int'我使用了 str.contains 但它给了我这个错误: &: 'str' and 'int' unsupported operand type(s)

This is my code:这是我的代码:

df['VVD'] = df[df['hashtags'].str.contains('rutte', 'mark', 'vvd')]

I know this is not the correct way of doing this but I'm struggling to find out how I can do this.我知道这不是正确的方法,但我正在努力找出如何做到这一点。

You are assigning a column to a dataframe!您正在为数据框分配一列!

Try this:尝试这个:

cond = df['hashtags'].str.contains('rutte|mark|vvd')
df['VVD'] = df.loc[cond, 'hashtags']

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据字符串在新列中添加值包含在另一列中 - adding values in new column based on string contains in another column Python - 使用正则表达式从 Pandas DataFrame 中的列(包含字符串)中提取权重并将其添加到新列中 - Python - Extracting weight from a column (containing a string) in a Pandas DataFrame using regex and adding it to a new column 移动行值包含特定字符串到 Python 中的新列 - Moving row values contains specific string to new column in Python 为使用 Python 的值求和添加新列 - Adding a new column for Summation of values using Python 使用 pandas 字符串包含和循环创建新类别列 - create new category column using pandas string contains and loops 使用 python 模式将字符串传输到新列 - Transfer a string to a new column using python pattern Pandas Python,如何搜索列以检查它是否包含某个字符串值,以便它在新列中返回一个值 - Pandas Python, how to search a column to check if it contains a certain string value so it returns a value in a new column 如果字符串“包含”substring,则添加带有条件的新列? - Add a new column with condition if a string 'contains' substring? 如果字符串“包含”子字符串,则创建一个带有条件的新列? - Create a new column with condition if a string 'contains' substring? 如果 DataFrame 包含特定字符串,则创建新列 - Create new column if DataFrame contains specific string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM