简体   繁体   English

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

[英]pandas DataFrame "no numeric data to plot" error

I have a small DataFrame that I want to plot using pandas.我有一个小的 DataFrame,我想使用 pandas 将其转换为 plot。

    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 中学习绘图。我想要一个 plot 在上面的示例中,当我说的时候。

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 的值非常不平衡使得 plot 变得非常无趣。我想知道为什么错误消息抱怨 plot 没有数字数据。

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

df=df.astype(float)

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.首先让我用pandasnumpy创建一个简单的数据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

受 alex314159 的启发,如果您在同一个表中还有浮动以外的其他数据

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

Convert non numeric data into numeric using:使用以下方法将非数字数据转换为数字:

DataFrame["Column_name"] = DataFrame["Column_name"].str.replace("[\$\,\.]", "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM