4

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.

4
  • try df['hashtags'].str.contains("rutte|mark|vvd") Commented May 7, 2021 at 23:33
  • Thankyou! I think this is almost it, but now the new column just contains true or false and I want it to contain the text Commented May 8, 2021 at 0:49
  • hi @Sophia, kindly add an example dataframe with expected output. Easier to work with visuals and give a more detailed/appropriate solution. Kindly share data, not pics. Commented May 8, 2021 at 1:22
  • @Sophia did you pass the boolean index back into df? df['VVD'] = df[df['hashtags'].str.contains("rutte|mark|vvd")] Commented May 8, 2021 at 2:20

1 Answer 1

2

You are assigning a column to a dataframe!

Try this:

cond = df['hashtags'].str.contains('rutte|mark|vvd')
df['VVD'] = df.loc[cond, 'hashtags']
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.