在数据框的每一行中应用textblob [英] Apply textblob in for each row of a dataframe
问题描述
我有一个带有col的数据框,其中有文本.我想应用textblob并计算每一行的情感价值.
i have a data frame with a col which has text. I want to apply textblob and calculate sentiment value for each row.
text sentiment
这太好了
好电影
很棒的故事
this is great
great movie
great story
当我执行以下代码时:
df['sentiment'] = list(map(lambda tweet: TextBlob(tweet), df['text']))
我得到了错误:
TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'float'>
如何将textBLob应用于数据帧中col的每一行以获取情绪值?
How do you apply textBLob to each row of a col in a dataframe to get the sentiment value?
推荐答案
您可以使用情感返回一个具有情感(极性,主观性)形式的命名元组.
Sentiment returns a namedtuple of the form Sentiment(polarity, subjectivity).
但是您确定df['text']
的每一行都是字符串格式吗?如果不是这样,如果TextBlob无法处理文本,您可以尝试在下面返回None
:
But are you sure each row of df['text']
is in string format? If not, you could try below to return None
if the text cannot be processed by TextBlob:
def sentiment_calc(text):
try:
return TextBlob(text).sentiment
except:
return None
df['sentiment'] = df['text'].apply(sentiment_calc)
这篇关于在数据框的每一行中应用textblob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!