How to Fix: ValueError: cannot convert float NaN to integer
Last Updated :
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 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:
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')
Similar Reads
How To Fix "Typeerror: Can'T Convert 'Float' Object To Str Implicitly" In Python In this article, we'll delve into understanding of typeerror and how to fix "Typeerror: Can'T Convert 'Float' Object To Str Implicitly" In Python. Types of Errors in Python Script ExecutionThere are many different types of errors that can occur while executing a python script. These errors indicate
3 min read
How To Fix Valueerror Exceptions In Python Python comes with built-in exceptions that are raised when common errors occur. These predefined exceptions provide an advantage because you can use the try-except block in Python to handle them beforehand. For instance, you can utilize the try-except block to manage the ValueError exception in Pyth
4 min read
How to Convert Integers to Floats in Pandas DataFrame? Pandas Dataframe provides the freedom to change the data type of column values. We can change them from Integers to Float type, Integer to String, String to Integer, etc. There are 2 methods to convert Integers to Floats: Method 1: Using DataFrame.astype() method Syntax :Â DataFrame.astype(dtype, co
4 min read
How to Convert NumPy Array of Floats into Integers In this article, we will see how to convert NumPy Array of Floats into Integers. We are given a NumPy array of float-type values. Our task is to convert all float-type values of Numpy array to their nearest array of integer values.Input: [1.2, 4.5, 9.1, 6.5, 8.9, 2.3, 1.2]Output: [1, 4, 9, 6, 8, 2,
4 min read
How to convert Float to Int in Python? In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4
5 min read
Cannot Convert String To Float in Python Python, a versatile and powerful programming language, is widely used for data manipulation and analysis. However, developers often encounter challenges, one of which is the "Cannot Convert String To Float" error. This error occurs when attempting to convert a string to a float, but the string's con
3 min read
How to Fix: TypeError: ânumpy.floatâ object is not callable? In this article, we are going to see how to fix TypeError: ânumpy.floatâ object is not callable in Python. There is only one case in which we can see this error: If we try to call a NumPy array as a function, we are most likely to get such an error. Example: Python3 import numpy as np a = np.array
1 min read
How to Convert float64 Columns to int64 in Pandas? float64 represents a floating-point number with double precision and int64 represents a 64-bit integer number. In this article, we will learn to Convert float64 Columns to int64 in Pandas using different methodsConvert float64 Columns to int64 in Pandas DataFrameTo transform a Pandas column to an in
3 min read
Convert Floats to Integers in a Pandas DataFrame Let us see how to convert float to integer in a Pandas DataFrame. We will be using the astype() method to do this. It can also be done using the apply() method. Convert Floats to Integers in a Pandas DataFrameBelow are the ways by which we can convert floats to integers in a Pandas DataFrame: Using
3 min read
How to Fix "TypeError: 'float' object is not callable" in Python In Python, encountering the error message "TypeError: 'float' object is not callable" is a common issue that can arise due to the misuse of the variable names or syntax errors. This article will explain why this error occurs and provide detailed steps to fix it along with the example code and common
3 min read