How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function? 2

How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function?

The “TypeError: no numeric data to plot” issue in Pandas occurs when attempting to use the Fix function on data that is not in a numeric format. To resolve this issue, the data must first be converted to a numeric format using the appropriate tools, such as the astype() function. Once the data is in a numeric format, the Fix function can be applied successfully. This will allow for the data to be plotted and visualized in an accurate manner.

Fix in Pandas: TypeError: no numeric data to plot


One error you may encounter when using pandas is:

TypeError: no numeric data to plot

This error occurs when you attempt to plot values from a pandas DataFrame, but there are no numeric values to plot.

This error typically occurs when you think a certain column in the DataFrame is numeric but it turns out to be a different data type.

The following example shows how to fix this error in practice.

How to Reproduce the Error

Suppose we have the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'team': ['A', 'A', 'B', 'B', 'B'],
                   'points': ['5', '7', '7', '9', '12'],
                   'rebounds': ['11', '8', '10', '6', '6'],
                   'blocks': ['4', '7', '7', '6', '5']})

#view DataFrame
df

	team	points	rebounds blocks
0	A	5	11	 4
1	A	7	8	 7
2	B	7	10	 7
3	B	9	6	 6
4	B	12	6	 5

Now suppose we attempt to create a line plot for the three variables that we believe are numeric: points, rebounds, and blocks:

#attempt to create line plot for points, rebounds, and blocks
df[['points', 'rebounds', 'blocks']].plot()

ValueError: no numeric data to plot

We receive an error because none of these columns are actually numeric.

How to Fix the Error

We can use the dtypes function to see what data type each column is in our DataFrame:

#display data type of each column in DataFrame
df.dtypes

team        object
points      object
rebounds    object
blocks      object
dtype: object

We can see that none of the columns in the DataFrame are numeric.

We can use the .astype() function to convert specific columns to numeric:

#convert points, rebounds, and blocks columns to numeric
df['points']=df['points'].astype(float)
df['rebounds']=df['rebounds'].astype(float)
df['blocks']=df['blocks'].astype(float)
#create line plot for points, rebounds, and blocks
df[['points', 'rebounds', 'blocks']].plot()

We’re able to successfully create a line plot for points, rebounds, and blocks because each variable is now numeric.

We can verify this by using the dtypes function once again:

#display data type of each column in DataFrame
df.dtypes

team         object
points      float64
rebounds    float64
blocks      float64
dtype: object

The following tutorials explain how to fix other common errors in Python:

Cite this article

stats writer (2024). How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-i-resolve-the-typeerror-no-numeric-data-to-plot-issue-in-pandas-when-trying-to-use-the-fix-function/

stats writer. "How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function?." PSYCHOLOGICAL SCALES, 11 May. 2024, https://scales.arabpsychology.com/stats/how-can-i-resolve-the-typeerror-no-numeric-data-to-plot-issue-in-pandas-when-trying-to-use-the-fix-function/.

stats writer. "How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-i-resolve-the-typeerror-no-numeric-data-to-plot-issue-in-pandas-when-trying-to-use-the-fix-function/.

stats writer (2024) 'How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-i-resolve-the-typeerror-no-numeric-data-to-plot-issue-in-pandas-when-trying-to-use-the-fix-function/.

[1] stats writer, "How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, May, 2024.

stats writer. How can I resolve the “TypeError: no numeric data to plot” issue in Pandas when trying to use the Fix function?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top