Python pandas DataFrame“没有要绘制的数字数据”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31494870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:06:18  来源:igfitidea点击:

pandas DataFrame "no numeric data to plot" error

pythonpandasmatplotlib

提问by harijay

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

我有一个小的 DataFrame,我想使用 Pandas 进行绘制。

    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 .

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

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 的价值非常不平衡,但情节非常无趣。我想知道为什么错误消息抱怨没有要绘制的数字数据。

回答by alex314159

Try the following before plotting:

在绘图之前尝试以下操作:

df=df.astype(float)

回答by chrys111

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 pandasand numpyto understand it better.

要解决此问题,您必须将要使用的特定列或列转换为数字。首先让我创建一个简单的数据框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