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

24 Dec, 2021
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 pandasimport pandas # import numpyimport numpy # create a dataframedataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',                                       'sridevi', 'vijay', 'sreemukhi'],                              'marks': [90.3, numpy.nan, 67.8, 89, numpy.nan]}) # convert to integer typedataframe['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
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 pandasimport pandas # import numpyimport numpy # create a dataframedataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',                                       'sridevi', 'vijay',                                       'sreemukhi'],                              'marks': [90.3, numpy.nan, 67.8, 89, numpy.nan]})# display data typeprint(dataframe['marks'] .dtype)  # drop the NaN valuesdataframe = dataframe.dropna() # displayprint(dataframe) # convert to integer type for marks columndataframe['marks'] = dataframe['marks'].astype(int) # display data typedataframe['marks'] .dtype
Output:
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 pandasimport pandas # import numpyimport numpy # create a dataframedataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',                                       'sridevi', 'vijay',                                       'sreemukhi'],                              'marks': [90.3, numpy.nan, 67.8, 89, numpy.nan]})# display data typeprint(dataframe['marks'] .dtype) # replace NaN values with 0dataframe = dataframe.fillna(0) # displayprint(dataframe) # convert to integer type for marks columndataframe['marks'] = dataframe['marks'].astype(int) # display data typedataframe['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 modulesimport numpy # create an nan valuedata = numpy.nan # displayprint(data) # convert man to valuefinal = numpy.nan_to_num(data) # displayfinal
Output:
nan
0.0
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 pandasimport pandas # import numpyimport numpy # create a dataframedataframe = pandas.DataFrame({'name': ['sireesha', 'gnanesh',                                       'sridevi', 'vijay',                                       'sreemukhi'],                              'marks': [90.3, numpy.NaN, 67.8, 89, numpy.NaN]})# display data typeprint(dataframe['marks'] .dtype) # replace NaN values with 0dataframe = dataframe.fillna(0) # displayprint(dataframe) # convert to integer type for marks columndataframe['marks'] = dataframe['marks'].astype(int) # display data typedataframe['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')
sagar0719kumar
Picked
Python How-to-fix
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Get unique values from a list
Python | datetime.timedelta() function