【发布时间】:2020-02-09 15:45:52
【问题描述】:
基本上我希望程序在数据帧中的行上运行 问题是当将每一行的每个处理值写入 csv 文件时,这些值将在所有行中重复,并且循环中的最后一行结果将覆盖其余值 这是我的代码:
#Import the libraries
from textblob import TextBlob
import pandas as pd
read=pd.read_csv('HR.csv',delimiter=',',skip_blank_lines=False,skiprows=1,names=['Comments','Score','Sentiment'])
data=pd.DataFrame(read)
for row in data.itertuples():
rw=str(row)
obj = TextBlob(rw)
sentiment=obj.sentiment.polarity
print(sentiment)
data['Score']=sentiment
data.to_csv('HR12.csv',index=False,mode='a')
if sentiment == 0:
s='Neutral'
data['Sentiment']='Neutral'
data.to_csv('HR12.csv',index=False)
elif sentiment > 0:
s='Positive'
data['Sentiment']='Positive'
data.to_csv('HR12.csv',index=False)
else:
s='Negative'
data['Sentiment']='Negative'
data.to_csv('HR12.csv',index=False)
因此,我没有获取在我的输出文件中输入的每一行的每个情绪,而是获取整个数据帧的唯一最后一个值 例如 : 评论、分数、情绪 d1,1,正 d2,1,正 d3,1,正 等等。
而不是 评论、分数、情绪 d1,-1,负 d2,1,正 d3,1,正 等等。
【问题讨论】: