我想在 Power BI 桌面中使用 Python 的 Textblob 进行情感分析。下面的代码用于创建一个单独的数据帧,我可以使用极性分数对其进行过滤。
# '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)
但是,我想直接写入现有数据表,例如
#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)
关于python - 使用 Python 进行 Power BI 中的极性和情感分析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60197761/