Python 我在进行情绪分析时遇到一个类型错误,如何解决此问题?

Python 我在进行情绪分析时遇到一个类型错误,如何解决此问题?,python,anaconda,jupyter,typeerror,sentiment-analysis,Python,Anaconda,Jupyter,Typeerror,Sentiment Analysis,我正在对比特币新闻进行情绪分析。在我编写代码的过程中,出现了一个类型错误问题。我希望你能帮助我,并提前感谢你 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 impor

我正在对比特币新闻进行情绪分析。在我编写代码的过程中,出现了一个类型错误问题。我希望你能帮助我,并提前感谢你

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)
然后出现以下类型错误:
您需要调试代码。您正在传递
None

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

news['polarity'] = news.apply(lambda x: TextBlob(x['description']).sentiment.polarity, axis=1)
确保在预处理阶段,
dataframe

中没有任何
None
NaN
值,正如正确指出的那样,
description
列中的某些行具有导致异常的
None
值。共有四行,请参见下面的屏幕截图

您应该删除这些行,并对具有正确数据的行进行操作。您可以在
description
列中使用

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

其他栏目可能需要重复上述内容。

非常感谢您的回答!我尝试了代码,再次遇到错误消息。。。C:\Users\matth\Anaconda3\lib\site packages\ipykernel\u launcher.py:2:SettingWithCopyWarning:试图在数据帧切片的副本上设置值。尝试使用.loc[row\u indexer,col\u indexer]=value,请参见文档中的警告:这是一个警告,您现在可以忽略。好的,谢谢,它现在可以工作了,但问题是过滤后只剩下19个ArtLice,而不是开始时的100个,这是否意味着81个描述中包含非值?正如您在屏幕截图中看到的,只有4行的
description
为None,其余96行的值正确。检查您是否遗漏了其他内容。