[英]Sentiment analysis using python textblob on a excel file data
我有一個問題,我需要計算 excel 文件中存在的兩列的情緒分析,並且在計算這兩列的極性之后,我需要更新已經存在於相同 excel 輸入中的其他兩列中的極性值文件。 我如何通過計算單個文本句子的極性來實現。 需要建議來計算 excel 文件中存在的整個列的極性。 我正在使用 pandas 進行 excel 處理。
from textblob import TextBlob
import pandas as pd
Input_file='filepath'
df = pd.read_excel(Input_file,
sheet_name='Sheet1')
col1 = pd['video_title'].tolist()
# col2 = pd['description'].tolist()
blob = TextBlob(col1)
# blob1 = Texxtblob(col2)
polarity_score = blob.sentiment.polarity
polarity_rounded = round(polarity_score, 6)
print(polarity_rounded)
正如我在上圖中發布的那樣,在這里我需要將“title_sentiment”列中的“None”值替換為計算的極性值。 同樣,我必須將“description_sentiment”列更新為計算出的極性值。
讓我們將您的情緒分析內容黑箱化並將您的問題減少到
我有一個 dataframe 和一個文本列,我想將 function 應用到,並將結果存儲為正確行中的新數字列。
竊取此人的示例 dataframe並帶有文本列以開始:
In [1]: import pandas as pd
...:
...: df = pd.DataFrame({
...: 'title': ['foo','bar','baz','baz','foo','bar'],
...: 'contents':[
...: 'Lorem ipsum dolor sit amet.',
...: 'Lorem ipsum dolor sit amet.',
...: 'Lorem ipsum dolor sit amet.',
...: 'Consectetur adipiscing elit.',
...: 'Lorem ipsum dolor sit amet.',
...: 'Lorem ipsum dolor sit amet.'
...: ],
...: 'year':[2010,2011,2000,2005,2010,2011]
...: })
...:
...: df
Out[1]:
title contents year
0 foo Lorem ipsum dolor sit amet. 2010
1 bar Lorem ipsum dolor sit amet. 2011
2 baz Lorem ipsum dolor sit amet. 2000
3 baz Consectetur adipiscing elit. 2005
4 foo Lorem ipsum dolor sit amet. 2010
5 bar Lorem ipsum dolor sit amet. 2011
現在我們要定義一個 function 以應用於“內容”並將結果存儲在一個新列中。 為此,我們可以使用pd.Series.apply()
:
In [2]: def sentiment_function(text):
...: # Put all your fancy sentiment stuff here; I will just use `len` as a dummy function.
...: return len(text)
...:
...: df['sentiment_score'] = df['contents'].apply(sentiment_function)
...: df
Out[2]:
title contents year sentiment_score
0 foo Lorem ipsum dolor sit amet. 2010 27
1 bar Lorem ipsum dolor sit amet. 2011 27
2 baz Lorem ipsum dolor sit amet. 2000 27
3 baz Consectetur adipiscing elit. 2005 28
4 foo Lorem ipsum dolor sit amet. 2010 27
5 bar Lorem ipsum dolor sit amet. 2011 27
您可以為您的兩列title_sentiment
和description_sentiment
執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.