Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在数据帧的每一行上应用IntensityAnalyzer函数&;提供情绪分数_Python_Python 3.x_Pandas - Fatal编程技术网

Python 在数据帧的每一行上应用IntensityAnalyzer函数&;提供情绪分数

Python 在数据帧的每一行上应用IntensityAnalyzer函数&;提供情绪分数,python,python-3.x,pandas,Python,Python 3.x,Pandas,我用Python编写了一个小程序,从nltk.thousion.vader库中调用thousionIntensityAnalyzer函数。我想分析c2栏中提到的评论,并在新的相邻栏中提供情绪分数。有10000条评论&我的评论在comments.txt文件中。我已经创建了get_touction()函数,但将数据帧的每一行作为参数传递并使用for循环调用它以提供情绪分数并将其存储在相邻的列中时遇到了问题 我试过这些代码: df['add'] = df.apply(lambda row: get_s

我用Python编写了一个小程序,从
nltk.thousion.vader
库中调用
thousionIntensityAnalyzer
函数。我想分析c2栏中提到的评论,并在新的相邻栏中提供情绪分数。有10000条评论&我的评论在
comments.txt
文件中。我已经创建了
get_touction()
函数,但将数据帧的每一行作为参数传递并使用for循环调用它以提供情绪分数并将其存储在相邻的列中时遇到了问题

我试过这些代码:

df['add'] = df.apply(lambda row: get_sentiment(row)) 
在internet上的任何位置都找不到解决方案。代码如下:

import nltk
import pandas as pd
import re
from nltk.sentiment.vader import SentimentIntensityAnalyzer
remarks = pd.read_csv('/Users/ZKDN0YU/Desktop/comments/Comments.txt', 
sep='\t')
remarks.head(50)
df = pd.DataFrame(remarks)
print(df)
def get_sentiment(remarks):
 sentiment_score = sid.polarity_scores(row)
 positive_meter = round((sentiment_score['pos'] * 10), 2)
 negative_meter = round((sentiment_score['neg'] * 10), 2)
 return positive_meter, negative_meter

for index, row in df.iterrows():
 df['add'] = df.apply(lambda row: get_sentiment(row)) 
 print(row['c1'], row['c2'],"Positive",positive_meter,"Negative", 
 negative_meter)
运行上述代码时出现以下错误:

File "<ipython-input-9-7223b4fb6bd7>", line 10, in get_sentiment
 sentiment_score = sid.polarity_scores(row)

NameError: ("name 'sid' is not defined", 'occurred at index c1')
文件“”,第10行,在get\u中 情绪得分=sid.极性得分(行) NameError:(“未定义名称‘sid’,‘发生在索引c1’) 尝试更换

对于索引,df.iterrows()中的行:
df['add']=df.apply(lambda行:get_情绪(行))
打印(第['c1']行、第['c2']行、“正片”、正片、负片),
负电(电表)

df['positive']=df.c2.apply(获取情绪,k='positive')
df['negative']=df.c2.apply(获取情绪,k='negative')
对于索引,df.iterrows()中的行:
打印(“正片:{},负片:{}”。格式(第['Positive']行,第['Negative']行)
在这里,我们将该函数应用于数据帧c2列中的所有值,并向数据帧中的新列“add”返回一个序列

get_情绪功能也需要使用此解决方案进行更新:

def get_情绪(第行,**kwargs):
#您以前的代码
如果kwargs['k']=='正数'或负数,则返回正数

您似乎忘记了定义
sid
,因此出现了错误
sid=EmperationIntensityAnalyzer()
可能会有帮助吗?明白了。。。已解决与sid有关的错误。处理其他错误。谢谢伟大的你有什么错误?我可能会更好地共享示例数据和预期输出,以便任何人都可以运行您的代码我尝试了上述代码:-df['add']=df.c2.apply(get_-touction)print(第['c1'],第['c2'],第['c2'],“正值”,正值,正值,负值,负值),但得到的错误文件是“C:\ProgramData\Anaconda3\lib\site packages\pandas\core\generic.py”,第5179行,在getattr返回对象中。uuu getattribute_uuu(self,name)AttributeError:“Series”对象没有属性“encode”,不知道我是否将正确的参数传递给get\u情感()函数。我已更新了答案。上一次的触发有点太快了。如果有用就试试(我现在不可能自己试试。)谢谢你的及时回复!在尝试代码后获取此错误。文件“C:\ProgramData\Anaconda3\lib\site packages\pandas\core\series.py”,第4028行,在f return func(x,*args,**kwds)TypeError:get_touction()得到一个意外的关键字参数“kwargs”,第三次是魅力。我已经再次更新了评论-现在测试。测试了代码…现在得到这个错误。文件“C:\ProgramData\Anaconda3\lib\site packages\nltk\emotional\vader.py”,第284行,在init text=str(text.encode('utf-8'))AttributeError中:“float”对象没有属性“encode”