首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python textblob对excel文件数据进行情感分析

使用python textblob对excel文件数据进行情感分析
EN

Stack Overflow用户
提问于 2020-12-07 19:47:57
回答 1查看 429关注 0票数 1

我有一个问题,我需要计算excel文件中存在的两个列的情感分析,并且在计算这两个列的极性之后,我需要更新其他两个列中已经存在于同一个excel输入文件中的那些极性值。我是如何通过计算单个文本句子的极性来实现的。需要建议来计算整个列的极性存在于excel文件中。我正在使用pandas进行excel处理。

代码语言:javascript
复制
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‘列更新为计算出的极性值。

所需输出:

EN

回答 1

Stack Overflow用户

发布于 2020-12-07 20:58:59

让我们对你的情绪分析内容进行黑箱操作,并将你的问题减少到

我有一个包含文本列的数据帧,我想对其应用函数,并将结果作为新的数值列存储在正确的行中。

盗取带有文本列的this person's example dataframe开始:

代码语言:javascript
复制
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

现在我们想定义一个应用于"contents“的函数,并将结果存储在一个新列中。为此,我们可以使用pd.Series.apply()

代码语言:javascript
复制
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执行此操作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65181192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档