如何统计一个结果出现了多少次?

How to count how many times a result has occurred?

我是 运行 使用 Twitter 进行情绪分析的,我在以下方面遇到了一些困难: 计算我有多少 'Positive'、'Negative' 和 'Neutral' 个结果。

我将不胜感激任何帮助。

请看我的代码:

import tweepy
from textblob import TextBlob

consumer_key = ''
consumer_key_secret = ''

access_token = ''
access_token_secret = ''

auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)

auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

public_tweets = api.search('Whosebug')

for tweet in public_tweets:
    print(tweet.text)
    analysis = TextBlob(tweet.text)
    print(analysis.sentiment)
    if analysis.sentiment[0]>0:
        print ('Positive')
    elif analysis.sentiment[0]<0:
        print('Negative')
    else:
        print ('Neutral')

我认为您可以创建变量来跟踪数据中有多少标签。像这样:

pos, neg, neu = 0, 0, 0
for tweet in public_tweets:
    analysis = TextBlob(tweet.text)
    if analysis.sentiment[0]>0:
        pos += 1
    elif analysis.sentiment[0]<0:
        neg += 1
    else:
        neu += 1
print("positive: {}\nnegative: {}\nneutral: {}".format(pos,neg,neu))

关于result dataframe,我不确定你要保存什么样的数据,所以无法给出一个好的答案。