python - 循环检索 pandas.core.series.Series 中的情绪分析
问题描述
我有 47 篇新闻文章要从中提取情绪。它们是 JSON 格式(文章的日期、标题和正文)。我想要的只是使用 TextBlob 获取带有情绪的列表。到目前为止,我正在执行以下操作:
import json
import pandas
from textblob import TextBlob
appended_data = []
for i in range(1,47):
df0 = pandas.DataFrame([json.loads(l) for l in open('News_%d.json' % i)])
appended_data.append(df0)
appended_data = pandas.concat(appended_data)
doc_set = appended_data.body
docs_TextBlob = TextBlob(doc_set)
for i in docs_TextBlob:
print(docs_TextBlob.sentiment)
Obvioulsy,我收到以下错误:TypeError: The text argument passed to __init__(text) must be a string, not <class 'pandas.core.series.Series'>
关于如何使用情绪度量创建列表的任何想法?
解决方案
DataFrame
要在情绪中创建一个新列:
appended_data['sentiment'] = appended_data.body.apply(lambda body: TextBlob(body).sentiment)
推荐阅读
- python - Python,在类上使用相同名称的坏习惯
- javascript - 反应图像路径是正确的,但它没有找到/导入它
- android - 该应用程序没有出现在我的手机中
- git - 用旧的本地主分支覆盖当前的原始主分支
- python - 用两个不同的数据集重新训练你的 CNN 模型
- spring-boot - 我们可以在 Cordapp 中模拟“CordaRPCops”以进行测试吗?
- javascript - 如何根据类别将项目添加到数组中?
- acumatica - 在自定义屏幕中查看委托时需要帮助
- google-cloud-platform - 使用 CLI 在谷歌云平台上更新 GPU
- ios - 尝试使用循环一次更新 300 多个联系人时 CNMutableContact 内存泄漏的 MutableCopy