我正在对推文进行情绪分析。在计算推文的情绪之前,我已经制作了一个删除表情符号和一些特殊字符的算法。之后,没有表情符号和特殊字符的推文被放入带有情绪的数据框中。这是代码:
x = 0
a = 0
d = {}
for vertaling in df['text']:
bericht = re.sub('[^A-Za-z0-9]', ' ', df['text'].iloc[x])
bericht = re.sub(' +',' ', bericht)
translations = translator.translate([bericht], dest='en')
for translation in translations:
a = a + 1
print(a)
print(translation.origin)
analysis = TextBlob(translation.text)
print(analysis.sentiment)
x = x + 1
d[translation.origin] = analysis.sentiment
c = ['Tweets','Sentiment']
df2 = pd.DataFrame(list(d.items()), columns=c)
我希望原始推文与计算出的情绪相结合。上面提供的代码将过滤后的推文与此特定行中的情绪相结合:
c = ['Tweets','Sentiment']
df2 = pd.DataFrame(list(d.items()), columns=c)
有没有人知道我可以将原始推文与数据框中新计算的情绪结合起来?
答案 0 :(得分:0)
没关系,我自己找到了解决方案。解决方案:
x = 0
a = 0
d = {}
#df2 = pd.DataFrame(['Tweets', 'Sentiment'])
df['Tweets'] = ""
df['Sentiment'] = ""
for vertaling in df['text']:
df['Tweets'].iloc[x] = df['text'].iloc[x]
bericht = re.sub('[^A-Za-z0-9]', ' ', df['text'].iloc[x])
bericht = re.sub(' +',' ', bericht)
translations = translator.translate([bericht], dest='en')
for translation in translations:
a = a + 1
print(a)
print(translation.origin)
analysis = TextBlob(translation.text)
df['Sentiment'].iloc[x] = analysis.sentiment
x = x + 1
d[translation.origin] = analysis.sentiment
这将新列与我现有的列组合在一起。