0

I have let's say following DF:


|Tweet|

|bla bla bad| 

|bla bla good|

From that I want to create something like:


|Tweet|           |Sentiment|

|bla bla bad|     |negative|

|bla bla good|    |positive|

I can search for string in a column with df[df['Tweet'].str.contains("bad|sad", na=False)] but how can I then create new column classifying them?

Thank you

BlueMango
  • 463
  • 7
  • 21

1 Answers1

2

Use numpy.where:

df['sentiment'] = np.where(df['Tweet'].str.contains('bad|sad'), 'negative', 'positive')
print(df)

Output

          Tweet sentiment
0   bla bla bad  negative
1  bla bla good  positive
Dani Mesejo
  • 61,499
  • 6
  • 49
  • 76