-4

I have a dataframe with a single column full_text containing tweets and there is a list negative containing negative words. I want to create a new column that returns a boolean value if the negative words are found in the tweets as 1 and 0 if not found.

  • I want to extract a parent dataframe of tweets, 2 sets of positive and negative tweets based on the list `negative` containing negative words. – Ambrish Dhaka Feb 16 '20 at 18:56

1 Answers1

0

Ok, let's assume we have a dataframe data and list negative_words like this:

data = pd.DataFrame({
    'Tweets' : ['This is bad', 'This is terrible', 'This is good', 'This is great'],
})

negative_words = ['bad', 'terrible']

We can then do something like:

1) We can use a lambda function with any:

# create lambda with any:
data['Negative'] = data.apply(lambda x: True if any(word in x.Tweets for word in negative_words) else False, axis=1)

And will get:

             Tweets  Negative
0       This is bad      True
1  This is terrible      True
2      This is good     False
3     This is great     False
Daniel
  • 3,228
  • 1
  • 7
  • 23