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/ 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 Answers
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.
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
Inspired by alex314159, if you have other data than float in the same table
df["YourColumnNameHere"]=df["YourColumnNameHere"].astype(float)
Convert non numeric data into numeric using:
DataFrame["Column_name"] = DataFrame["Column_name"].str.replace("[\$\,\.]", "")