pandas 为数据框的每一行应用 textblob
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43485469/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Apply textblob in for each row of a dataframe
提问by user2585048
i have a data frame with a col which has text. I want to apply textblob and calculate sentiment value for each row.
我有一个带有文本的 col 的数据框。我想应用 textblob 并计算每一行的情绪值。
text sentiment
this is great
great movie
great story
这是
一部很棒的电影很棒的故事
When i execute the below code:
当我执行以下代码时:
df['sentiment'] = list(map(lambda tweet: TextBlob(tweet), df['text']))
df['sentiment'] = list(map(lambda tweet: TextBlob(tweet), df['text']))
I get the error:
我收到错误:
TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'float'>
How do you apply textBLob to each row of a col in a dataframe to get the sentiment value?
如何将 textBLob 应用于数据帧中 col 的每一行以获取情绪值?
回答by JAV
You can use .apply:
您可以使用.apply:
df['sentiment'] = df['text'].apply(lambda tweet: TextBlob(tweet).sentiment)
Sentiment returns a namedtuple of the form Sentiment(polarity, subjectivity).
Sentiment 返回形式为 Sentiment(polarity, subjectivity) 的命名元组。
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:
但是你确定每一行df['text']
都是字符串格式吗?如果没有,None
如果 TextBlob 无法处理文本,您可以尝试在下面返回:
def sentiment_calc(text):
try:
return TextBlob(text).sentiment
except:
return None
df['sentiment'] = df['text'].apply(sentiment_calc)