Open In App

How to Fix: ValueError: cannot convert float NaN to integer

Improve
Improve
Like Article
Like
Save
Share
Report

In this article we will discuss how to fix the value error – cannot convert float NaN to integer in Python.

In Python, NaN stands for Not a Number. This error will occur when we are converting the dataframe column of the float type that contains NaN values to an integer.

Let’s see the error and explore the methods to deal with it.

Dataset in use:

Let’s check the error when converting from float type (marks column) to integer type. We can convert by using astype() function

Example: Depicting the error

Python3




# import pandas
import pandas
 
# import numpy
import numpy
 
# create a dataframe
dataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',
                                       'sridevi', 'vijay', 'sreemukhi'],
                              'marks': [90.3, numpy.nan, 67.8, 89, numpy.nan]})
 
# convert to integer type
dataframe['marks'].astype(int)


Output:

ValueError: Cannot convert non-finite values (NA or inf) to integer

Because the NaN values are not possible to convert the dataframe. So in order to fix this issue, we have to remove NaN values

Method 1: Drop rows with NaN values

Here we are going to remove NaN values from the dataframe column by using dropna() function. This function will remove the rows that contain NaN values.

Syntax:

dataframe.dropna()

Example: Dealing with error 

Python3




# import pandas
import pandas
 
# import numpy
import numpy
 
# create a dataframe
dataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',
                                       'sridevi', 'vijay',
                                       'sreemukhi'],
                              'marks': [90.3, numpy.nan, 67.8, 89, numpy.nan]})
# display data type
print(dataframe['marks'] .dtype)
 
 
# drop the NaN values
dataframe = dataframe.dropna()
 
# display
print(dataframe)
 
# convert to integer type for marks column
dataframe['marks'] = dataframe['marks'].astype(int)
 
# display data type
dataframe['marks'] .dtype


Output:

Method 2: Replace NaN values with 0

We can replace NaN values with 0 to get rid of NaN values. This is done by using fillna() function. This function will check the NaN values in the dataframe columns and fill the given value.

Syntax:

dataframe.fillna(0)

Example: Dealing with the error

Python3




# import pandas
import pandas
 
# import numpy
import numpy
 
# create a dataframe
dataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',
                                       'sridevi', 'vijay',
                                       'sreemukhi'],
                              'marks': [90.3, numpy.nan, 67.8, 89, numpy.nan]})
# display data type
print(dataframe['marks'] .dtype)
 
# replace NaN values with 0
dataframe = dataframe.fillna(0)
 
# display
print(dataframe)
 
# convert to integer type for marks column
dataframe['marks'] = dataframe['marks'].astype(int)
 
# display data type
dataframe['marks'] .dtype


Output:

Method 3: Using numpy.nan_to_num()

Here we are using NumPy to convert NaN values to 0 numbers.

Syntax:

numpy.nan_to_num(numpy.nal)

Example: Dealing with the error

Python3




# import modules
import numpy
 
# create an nan value
data = numpy.nan
 
# display
print(data)
 
# convert man to value
final = numpy.nan_to_num(data)
 
# display
final


Output:

nan
0.0

Method 4: Use Nullable 

We can create nan value as NaN, this does not create any error while converting float to integer.

Syntax:

numpy.NaN

Example: Dealing with the error

Python3




# import pandas
import pandas
 
# import numpy
import numpy
 
# create a dataframe
dataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',
                                       'sridevi', 'vijay',
                                       'sreemukhi'],
                              'marks': [90.3, numpy.NaN, 67.8, 89, numpy.NaN]})
# display data type
print(dataframe['marks'] .dtype)
 
# replace NaN values with 0
dataframe = dataframe.fillna(0)
 
# display
print(dataframe)
 
# convert to integer type for marks column
dataframe['marks'] = dataframe['marks'].astype(int)
 
# display data type
dataframe['marks'] .dtype


Output:

float64
        name  marks
0   sireesha   90.3
1    gnanesh    0.0
2    sridevi   67.8
3      vijay   89.0
4  sreemukhi    0.0
dtype('int64')


Last Updated : 24 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads