【发布时间】:2021-05-21 12:41:19
【问题描述】:
【问题讨论】:
-
请分享代码并输出为文本,而不是图像。
标签: python python-3.x pandas pandas-loc
【问题讨论】:
标签: python python-3.x pandas pandas-loc
尝试以下方法:
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 是一个方法名称,而不是您要查找的列。
【讨论】:
如果您尝试比较“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.compund() >0
【讨论】: