Tag Archives: TypeError: Empty ‘DataFrame’: no numeric data to plot

[Solved] Pandas Read CSV Error: TypeError: Empty ‘DataFrame’: no numeric data to plot

Simple code, using the panda module to read the CSV data file, there are two ways, one is abandoned by the new version of panda series. From_ csv; The other is panda. Read_ csv

First of all, the problem is that when reading a CSV file, the default data is object type, so there is no character type data to plot. At this time, you only need to convert the type, as follows:

from pandas import Series
import matplotlib.pyplot as plt
data = Series.from_csv('daily.csv',header=0)
#print(data.head())
data.astype(float)
data.plot()
plt.show()

To solve this problem perfectly, the following figure is drawn:

This is the desired result, so the other is panda. Read_ CSV, you are not so lucky. You will find this kind of data stored in CSV:

1981/1/1,20.7
1981/1/2,17.9
1981/1/3,18.8
1981/1/4,14.6
1981/1/5,15.8
1981/1/6,15.8
1981/1/7,15.8
1981/1/8,17.4
1981/1/9,21.8
1981/1/10,20
1981/1/11,16.2
1981/1/12,13.3
1981/1/13,16.7
1981/1/14,21.5

It can’t be displayed as X-axis label, so I changed the first column to 1981… To solve the problem. At this time, of course, we don’t recommend using the second method. If it’s so troublesome, we can use pandas to draw directly by PLT

Finally, we must pay attention to whether the data is abnormal. Some abnormal data are text exceptions, while some may be semantic exceptions. We need to mine and remove the abnormal data. This is often very important. Of course, drawing is often one of the more intuitive ways

Python Pandas TypeError: Empty ‘DataFrame’: no numeric data to plot

Tushare returns the dataframe format of panda, but an error occurs when executing the following code: typeerror: empty ‘dataframe’: no numeric data to plot

import tushare as ts

df_all = ts.realtime_boxoffice()
df_box_office = df['BoxOffice']
df_box_office.index = df['Irank']
df_box_office.plot(kind='bar')

Repeatedly output DF [‘boxoffice ‘] to confirm that it has value. I can’t figure out why it will report “empty”, and Baidu has no result

I thought there was a bug in the combination of tusahre and pandas. When I was ready to give up, I saw “numeric” in the error report. Does it mean that DF [‘boxoffice ‘] is a string type, not a numeric type, which leads to the error report

See the usage of “astype ()” in the official documents of panda, and change the code to the following successful drawing (in addition, use “ PD. To_ Numeric () “is OK, but I have to go back to panda from tushare. I feel that the code doesn’t look so smooth

import tushare as ts

df_all = ts.realtime_boxoffice()
df_box_office = df['BoxOffice'].astype(float)
df_box_office.index = df['Irank']
df_box_office.plot(kind='bar')