Python 为数据帧的每一行应用textblob

Python 为数据帧的每一行应用textblob,python,pandas,textblob,Python,Pandas,Textblob,我有一个带有col的数据框,其中包含文本。我想应用textblob并计算每行的情绪值 text sentiment 这太棒了 伟大的电影 伟大的故事 当我执行以下代码时: df['touction']=list(map(lambda tweet:TextBlob(tweet),df['text'])) 我得到一个错误: TypeError: The `text` argument passed to `__init__(text)` must be a strin

我有一个带有col的数据框,其中包含文本。我想应用textblob并计算每行的情绪值

text                sentiment
这太棒了
伟大的电影 伟大的故事

当我执行以下代码时:

df['touction']=list(map(lambda tweet:TextBlob(tweet),df['text']))

我得到一个错误:

TypeError: The `text` argument passed to `__init__(text)` must be a string, not <class 'float'>
TypeError:传递给`\uuu init\uuu(text)`的`text`参数必须是字符串,而不是字符串
如何将textBLob应用于数据帧中列的每一行以获取情绪值?

您可以使用:

情绪返回情绪形式(极性、主观性)的命名倍数

但是您确定df['text']的每一行都是字符串格式的吗?如果没有,如果文本无法由TextBlob处理,您可以在下面尝试返回
None

def sentiment_calc(text):
    try:
        return TextBlob(text).sentiment
    except:
        return None

df['sentiment'] = df['text'].apply(sentiment_calc)
def sentiment_calc(text):
    try:
        return TextBlob(text).sentiment
    except:
        return None

df['sentiment'] = df['text'].apply(sentiment_calc)