Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pandas DataFrame "no numeric data to plot" error

I have a small DataFrame that I want to plot using 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 .

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.

like image 967
harijay Avatar asked Jul 18 '15 19:07

harijay


People also ask

How do you convert non-numeric data to numeric data in Python?

To encode non-numeric data to numeric you can use scikit-learn's LabelEncoder. It will encode each category such as COL1's a , b , c to integers. enc. fit() creates the corresponding integer values.

How does Python handle non-numeric data?

First, you will want to cycle through the columns in the Pandas dataframe. For columns that are not numbers, you want to find their unique elements. This can be done by simply take a set of the column values. From here, the index within that set can be the new "numerical" value or "id" of the text data.

How do I convert items to numeric in pandas?

to_numeric() The best way to convert one or more columns of a DataFrame to numeric values is to use pandas. to_numeric(). This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

What is the default plot () type in pandas plotting?

With a DataFrame , pandas creates by default one line plot for each of the columns with numeric data.


3 Answers

Try the following before plotting:

df=df.astype(float)
like image 57
alex314159 Avatar answered Oct 11 '22 22:10

alex314159


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.

#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
like image 42
chrys111 Avatar answered Oct 11 '22 22:10

chrys111


Inspired by alex314159, if you have other data than float in the same table

df["YourColumnNameHere"]=df["YourColumnNameHere"].astype(float)
like image 7
Punnerud Avatar answered Oct 11 '22 22:10

Punnerud