使用 Python 在 Power BI 中进行极性和情感分析

Polarity and Sentiment Analysis in Power BI with Python

我想使用 Python 的 Textblob 在 Power BI 桌面中进行情绪分析。下面的代码用于创建一个单独的数据帧,我可以用极性分数过滤它。

# '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)

不过,我想直接写入现有数据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'])

参考:https://www.absentdata.com/power-bi/sentiment-analysis-in-power-bi/

我假设您可以执行类似的操作并获得与您在第一个脚本中获得的类似字段。

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)