80

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.

4
  • 5
    What data types do you see if you run df.dtypes? Commented Jul 18, 2015 at 19:51
  • 2
    Your DataFrame probably contains numeric strings. It doesn't look like the columns were correctly parsed -- you might have just one column called '2 3'. To help you fix this we'd probably need to see how df was defined. Commented Jul 18, 2015 at 19:58
  • Also, looks like you may have row-oriented data and pandas is going to expect column-oriented, you might want to transpose. Commented Jul 18, 2015 at 22:52
  • 4
    df.info() will give you the columns and data types of each column. Commented Jul 19, 2015 at 1:32

5 Answers 5

123

Try the following before plotting:

df=df.astype(float)

There's a lot of magic behind pandas, for instance, when you use pandas.read_csv to read a file. In particular, it has to infer the data type. Sometimes it gets it wrong. The code above forces pandas to try and convert the data to floating point numbers.

Sign up to request clarification or add additional context in comments.

4 Comments

I wish an explanation was provided for solutions like these. If someone comes about my comment and understands this answer fully, please provide an explanation.
@GregHilston there's a lot of magic behind pandas for instance when you use pandas.read_csv to read a file. In particular it has to infer the data type. Sometimes it gets it wrong. My answer above forces pandas to try and convert the data to floating point numbers.
@EdekiOkoh Hey there! Yea, this does seem very straight forward to me now. At the time, I was brand new to Pandas and did not understand what was happening at all. I'm with you now, but people who are new might be in the same boat I once was. Happy coding!
my data inside the df are strings. I just want a table. I also get this error. Any ideas?
15

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

3 Comments

Regarding pf.id=pd.to_numeric(pf.id), how do you do this if the column header has more than one word in it? For example, I have a column named 'Percent Working'.
@KesPerron try this: pf['Percent Working']
my data inside the df are strings. I just want a table. I also get this error. Any ideas?
8

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

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

1 Comment

my data inside the df are strings. I just want a table. I also get this error. Any ideas?
1

Convert non numeric data into numeric using:

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

Comments

0

It happens because the column you are trying to plot contains non-numeric data such as string. You need to convert such columns using:

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

If the value contains characters like ' or " this will fail. In that case you, need to add one more line of code before that code:

df["column_name"]=df["column_name"].str.replace(',', '')
df["column_name"]=df["column_name"].astype(float)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.