在进行情感分析时出现TypeError,如何解决此问题?

问题描述 投票:-1回答:2

我正在对比特币新闻进行情感分析。在我编码期间,发生TypeError问题。希望您能帮到我,非常感谢!

from newsapi.newsapi_client import NewsApiClient
from textblob import TextBlob
import pandas as pd
import numpy as np
from bs4 import BeautifulSoup
import datetime
from datetime import time
import csv
from dateutil import parser

api = NewsApiClient(api_key='my key')

all_articles = api.get_everything(q='bitcoin',
                                      sources='bbc-news,the-verge,financial-times,metro,business-insider,reuters,bloomberg,cnbc,cbc-news,fortune,crypto-coins-news',
                                      domains='bbc.co.uk,techcrunch.com',
                                      from_param='2019-10-20',
                                      to='2019-11-19',
                                      language='en')
                                      sort_by='relevancy',
                                    page_size=100)

news= pd.DataFrame(all_articles['articles'])

news['polarity'] = news.apply(lambda x: TextBlob(x['description']).sentiment.polarity, axis=1)
news['subjectivity'] = news.apply(lambda x: TextBlob(x['description']).sentiment.subjectivity, axis=1)
news['date']= news.apply(lambda x: parser.parse(x['publishedAt']).strftime('%Y.%m.%d'), axis=1)
news['time']= news.apply(lambda x: parser.parse(x['publishedAt']).strftime('%H:%M'), axis=1)

然后发生此TypeError:enter image description here

python anaconda jupyter typeerror sentiment-analysis
2个回答
0
投票

正如@Hayat正确指出的那样,description列中的某些行具有None值,这导致了异常。见下文。

Rows with <code>None</code> in <code>description</code> column

您应删除此类行,并对具有适当数据的行进行操作。您可以使用

过滤此类行
news_filtered = news[news['description'].notnull()]
news_filtered['polarity'] = news_filtered.apply(lambda x: TextBlob(x['description']).sentiment.polarity, axis=1)

您可能需要对其他列重复以上操作。


-1
投票

您需要调试代码。您正在传递None值。

[x['description']中可能有一些None值。

news['polarity'] = news.apply(lambda x: TextBlob(x['description']).sentiment.polarity, axis=1)

请确保在预处理阶段中None中没有任何NaNdataframe

© www.soinside.com 2019 - 2024. All rights reserved.