Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'

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.

like image 859
Sophia Avatar asked Sep 01 '25 17:09

Sophia


1 Answers

You are assigning a column to a dataframe!

Try this:

cond = df['hashtags'].str.contains('rutte|mark|vvd')
df['VVD'] = df.loc[cond, 'hashtags']
like image 68
Code Different Avatar answered Sep 04 '25 05:09

Code Different