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.
Asked
Active
Viewed 851 times
-4

Ambrish Dhaka
- 25
- 6
-
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 Answers
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
-
thanks! that seems robust. But can we reduce it two just 1 line of code – Ambrish Dhaka Feb 16 '20 at 18:57
-
Can we use the lambda function if 'Tweets` is a list instead of dataframe? – Ambrish Dhaka Feb 18 '20 at 03:07
-
`text4b = list(filter(lambda x: negative in x, text))` It gives error. Can somebody help? – Ambrish Dhaka Feb 18 '20 at 03:23
-
`text` is the list of tweets containing more than 50,000 tweets. – Ambrish Dhaka Feb 18 '20 at 03:24
-
You can do: `data = pd.DataFrame({'Tweets' : ['tweet1', 'tweet2' ...]})` to convert the list to a dataframe – Daniel Feb 18 '20 at 03:25
-
sorry, I couldn't get that. But, can we get the iteration over list in my code right. It's giving error. – Ambrish Dhaka Feb 18 '20 at 03:29
-
something like `text4b = list(filter(lambda x: (for word in negative for word in x), text)) ` – Ambrish Dhaka Feb 18 '20 at 03:33
-
Thanks for the solution @Daniel, Im working with something similar but hashes, How could I remove the words from the columns Tweets, instead of returning True/False? – Geovani Benita Nov 22 '22 at 16:09