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 numpy
dataframe = pandas.DataFrame({ 'name' : [ 'sireesha' , 'gnanesh' ,
'sridevi' , 'vijay' , 'sreemukhi' ],
'marks' : [ 90.3 , numpy.nan, 67.8 , 89 , numpy.nan]})
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 numpy
dataframe = pandas.DataFrame({ 'name' : [ 'sireesha' , 'gnanesh' ,
'sridevi' , 'vijay' ,
'sreemukhi' ],
'marks' : [ 90.3 , numpy.nan, 67.8 , 89 , numpy.nan]})
print (dataframe[ 'marks' ] .dtype)
dataframe = dataframe.dropna()
print (dataframe)
dataframe[ 'marks' ] = dataframe[ 'marks' ].astype( int )
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 numpy
dataframe = pandas.DataFrame({ 'name' : [ 'sireesha' , 'gnanesh' ,
'sridevi' , 'vijay' ,
'sreemukhi' ],
'marks' : [ 90.3 , numpy.nan, 67.8 , 89 , numpy.nan]})
print (dataframe[ 'marks' ] .dtype)
dataframe = dataframe.fillna( 0 )
print (dataframe)
dataframe[ 'marks' ] = dataframe[ 'marks' ].astype( int )
dataframe[ 'marks' ] .dtype
|
Output:

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 numpy
data = numpy.nan
print (data)
final = numpy.nan_to_num(data)
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 numpy
dataframe = pandas.DataFrame({ 'name' : [ 'sireesha' , 'gnanesh' ,
'sridevi' , 'vijay' ,
'sreemukhi' ],
'marks' : [ 90.3 , numpy.NaN, 67.8 , 89 , numpy.NaN]})
print (dataframe[ 'marks' ] .dtype)
dataframe = dataframe.fillna( 0 )
print (dataframe)
dataframe[ 'marks' ] = dataframe[ 'marks' ].astype( int )
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')