pandas DataFrame“无数字数据绘图”错误

时间:2015-07-18 19:38:21

标签: python pandas matplotlib

我想要使用pandas绘制一个小的DataFrame。

    2   3
0   1300    1000
1   242751149   199446827
2   237712649   194704827
3   16.2    23.0

我仍在尝试从熊猫中学习绘图。我想要一个情节在上面的例子中,当我说。

df.plot()

我得到了最奇怪的错误。

Library/Python/2.7/site-packages/pandas-0.16.2-py2.7-macosx-10.10-intel.egg/pandas/tools/plotting.pyc in _compute_plot_data(self)
   1015         if is_empty:
   1016             raise TypeError('Empty {0!r}: no numeric data to '
-> 1017                             'plot'.format(numeric_data.__class__.__name__))
   1018 
   1019         self.data = numeric_data

TypeError: Empty 'DataFrame': no numeric data to plot

虽然我理解DataFrame具有非常不平衡的值会产生一个非常有趣的情节。我想知道为什么错误消息抱怨没有数字数据要绘制。

3 个答案:

答案 0 :(得分:68)

在绘图前尝试以下操作:

df=df.astype(float)

答案 1 :(得分:2)

要解决此问题,您必须转换要转换的特定列     用于数字。首先,让我用pandasnumpy创建一个简单的数据框     更好地了解它。

#creating the dataframe

import pandas as pd
import numpy as np
details=[['kofi',30,'male',1.5],['ama',43,'female',2.5]]
pf=pd.DataFrame(np.array(details),[0,1],['name','age','sex','id'])

pf  #here i am calling the dataframe

   name age     sex   id
0  kofi  30    male  1.5
1   ama  43  female  2.5

#to make your plot work you need to convert the columns that have numbers into numeric
as seen below 

pf.id=pd.to_numeric(pf.id)
pf.age=pd.to_numeric(pf.age)

pf.plot.scatter(x='id',y='age')

#This should work perfectly

答案 2 :(得分:1)

受alex314159的启发,如果您在同一表中除了float之外还有其他数据

df["YourColumnNameHere"]=df["YourColumnNameHere"].astype(float)