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

时间:2022-07-14 23:48:28

I have a small DataFrame that I want to plot using pandas.

我有一个小的DataFrame,我想用熊猫绘图。

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

I am still trying to learn plotting from within pandas . I want a plot In the above example when I say .

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

df.plot()

I get the strangest error.

我得到了最奇怪的错误。

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

While I understand that the DataFrame with its very lopsided values makes a very un-interesting plot. I am wondering why the error message complains of no numeric data to plot.

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

2 个解决方案

#1


53  

Try the following before plotting:

在绘图之前尝试以下方法:

df=df.astype(float)

#2


0  

To solve this you have to convert the particular column or columns you want to use to numeric. First let me create a simple dataframe with pandas and numpy to understand it better.

要解决此问题,您必须将要使用的特定列转换为数字。首先让我创建一个带有pandas和numpy的简单数据框,以便更好地理解它。

#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

#1


53  

Try the following before plotting:

在绘图之前尝试以下方法:

df=df.astype(float)

#2


0  

To solve this you have to convert the particular column or columns you want to use to numeric. First let me create a simple dataframe with pandas and numpy to understand it better.

要解决此问题,您必须将要使用的特定列转换为数字。首先让我创建一个带有pandas和numpy的简单数据框,以便更好地理解它。

#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