[英]Polarity and Sentiment Analysis in Power BI with Python
I would like to use Python's Textblob for sentiment analysis in Power BI desktop.我想在 Power BI 桌面中使用 Python 的 Textblob 进行情感分析。 The code below works to create a separate dataframe that I can filter down to with the polarity scores.
下面的代码用于创建一个单独的数据框,我可以使用极性分数对其进行过滤。
# 'dataset' holds the input data for this script
import pandas as pd
from textblob import TextBlob
from itertools import islice
COLS = ['PersonID', 'QuestionID','Comment','subjectivity','polarity']
df = pd.DataFrame(columns=COLS)
for index, row in islice(dataset.iterrows(), 0, None):
new_entry = []
text_lower=str(row['Comment']).lower()
blob = TextBlob(text_lower)
sentiment = blob.sentiment
polarity = sentiment.polarity
subjectivity = sentiment.subjectivity
new_entry += [row['PersonID'], row['QuestionID'],row['Comment'],subjectivity,polarity]
single_survey_sentimet_df = pd.DataFrame([new_entry], columns=COLS)
df = df.append(single_survey_sentimet_df, ignore_index=True)
However, I would like to write directly to the existing data table like但是,我想直接写入现有的数据表,如
#load in our dependencies
import pandas as pd
from nltk.sentiment.vader import SentimentIntensityAnalyzer
#load in the sentiment analyzer
sia=SentimentIntensityAnalyzer()
#apply the analyzer over each comment added to the existing table
# **I WANT TO USE A LINE LIKE THE ONE BELOW, BUT WITH THE TEXTBLOB FUNCTIONALITY ABOVE**
dataset['polairty scores'] =dataset['Message'].apply(lambda x: sia.polarity_scores(x)['compound'])
Reference: https://www.absentdata.com/power-bi/sentiment-analysis-in-power-bi/参考: https : //www.absentdata.com/power-bi/sentiment-analysis-in-power-bi/
I assume you could do something like this and get similar fields that you got in your first script.我假设您可以执行这样的操作并获得与您在第一个脚本中获得的字段类似的字段。
dataset['polarity'] =dataset['Comment'].apply(lambda x: TextBlob(str(x).lower()).sentiment.polarity)
dataset['subjectivity'] =dataset['Comment'].apply(lambda x: TextBlob(str(x).lower()).sentiment.subjectivity)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.