如何解决TypeError:Empty'DataFrame':没有要绘制的数字数据?

时间:2017-05-01 08:39:03

标签: mongodb pandas pymongo

在MongoDB中,我有一个名为twitter的数据库。从数据库中,我试图提取出更多用于推文的5种语言。我想用图表显示结果。为此,我使用了pandasmatplotlib。但是在执行代码时,它会说TypeError: Empty 'DataFrame': no numeric data to plot

这是我的代码:

import pymongo
import pandas as pd
import matplotlib.pyplot as plt
#conect with MongoClient daemon mongod
client_con = pymongo.MongoClient()
#conect with db
mydb = client_con["twitter"]
#conect with collection
mycol = mydb["twitterCol"]
#print the records
print mycol.count()

#intiate pandas
tweets = pd.DataFrame()

tweets_data = []
tweets_data= mydb.mycol.find()

"""Next, 3 columns will be added to the tweets 
DataFrame called text, lang, and country. 
Text column contains the tweet, 
Lang column contains the language in which the 
tweet was written, 
and Country the country from which the tweet was sent.
"""

tweets['text'] = map(lambda tweet: tweet["text"], tweets_data)
tweets['lang'] = map(lambda tweet: tweet['lang'], tweets_data)
tweets['country'] = map(lambda tweet: tweet['place']['country'] if tweet['place'] != None else None, tweets_data)


tweets_by_lang = tweets['lang'].value_counts()
tweets_by_lang = tweets_by_lang.astype(int)
fig, ax = plt.subplots()
ax.tick_params(axis='x', labelsize=15)
ax.tick_params(axis='y', labelsize=10)
ax.set_xlabel('Languages', fontsize=15)
ax.set_ylabel('Number of tweets' , fontsize=15)
ax.set_title('Top 5 languages', fontsize=15, fontweight='bold')
tweets_by_lang.plot(ax=ax, kind='bar', color='red')

如何解决此问题?

0 个答案:

没有答案