Error: '>' not supported between instances of 'method' and 'int'

Error: '>' not supported between instances of 'method' and 'int'

此代码在 Jupyter 中不起作用 IDE。我找不到我的错误。请帮忙

我使用的数据框的前 5 行如下所示:

在第 1 行添加括号 df.compund() >0

如果您尝试比较 'compound' 列的值,则必须使用 df['compound'] 而不是 df.compound,这是一种方法。

也许下面的代码可以帮到您:

分类函数'Sentiment_Type'

def sentiment(score):
    if score < 0:
        return "Negative"
    elif score > 0:
        return "Positive"
    else:
        return "Neutral"

之后,您可以使用此功能创建新列

df['Sentiment_Type'] = df['compound'].apply(sentiment)

尝试以下操作:

df.loc[df['compound'] > 0,'SentimentType'] = 'Positive'
df.loc[df['compound'] < 0,'SentimentType'] = 'Negative'
df.loc[df['compound'] == 0,'SentimentType'] = 'Neutral'

您应该 df['compound'] 而不是通过 df.compound, 检索列。您还可以从收到的错误消息中得知 df.compound 是一个方法名称,而不是您要查找的列。