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