【问题标题】:Error: '>' not supported between instances of 'method' and 'int'错误:“方法”和“int”的实例之间不支持“>”
【发布时间】:2021-05-21 12:41:19
【问题描述】:

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

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

【问题讨论】:

  • 请分享代码并输出为文本,而不是图像。

标签: python python-3.x pandas pandas-loc


【解决方案1】:

尝试以下方法:

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 是一个方法名称,而不是您要查找的列。

【讨论】:

    【解决方案2】:

    如果您尝试比较“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)
    

    【讨论】:

      【解决方案3】:

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

      【讨论】:

      • 请再次检查问题。我已经添加了我正在使用的数据框的前五行。
      猜你喜欢
      • 1970-01-01
      • 2021-12-29
      • 1970-01-01
      • 2020-05-19
      • 2021-08-10
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多