【问题标题】:Sentiment analysis using python textblob on a excel file data使用python textblob对excel文件数据进行情感分析
【发布时间】:2020-12-07 11:47:56
【问题描述】:

我有一个问题,我需要计算 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”列更新为计算出的极性值。

期望的输出:

【问题讨论】:

  • 您能否举例说明您想要的结果的输入/输出?我们可能可以将其中的情绪部分黑箱化为一些函数,它提供一个数字并帮助您以您正在寻找的形式获得结果。
  • [1]: i.stack.imgur.com/qmKSm.png 正如我在上图中发布的那样,我需要将“title_sentiment”列中的“None”值替换为计算出的极性值。同样,我必须将“description_sentiment”列更新为计算出的极性值,如 0.1、0、0.23 等。
  • @user1717828 想要的输出喜欢 [2]:i.stack.imgur.com/EWRFu.png

标签: python excel pandas sentiment-analysis textblob


【解决方案1】:

让我们将您的情绪分析内容黑箱化并将您的问题减少到

我有一个带有文本列的数据框,我想将函数应用到该列,并将结果作为新的数字列存储在正确的行中。

使用文本栏窃取this person's example 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

现在我们要定义一个函数以应用于“内容”并将结果存储在一个新列中。为此,我们可以使用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_sentimentdescription_sentiment)执行此操作。

【讨论】:

  • 感谢@user1717828,在对我的代码以及您的代码 sn-ps 进行了几次修改后,我已经达到了预期的输出。
猜你喜欢
  • 2018-06-25
  • 1970-01-01
  • 2020-03-26
  • 1970-01-01
  • 2020-08-14
  • 1970-01-01
  • 2022-06-22
  • 2017-10-07
  • 1970-01-01
相关资源
最近更新 更多