valueerror: cannot convert float nan to integer ( Solved )

valueerror_ cannot convert float nan to integer

There are many times when a programmer raises an exception that is the ValueError. You can get this error when you give a invalid value to the functions. The error valueerror: cannot convert float nan to integer comes when you are applying a function on the columns containing float values with NaN in it. In this entire tutorial you will know the various way you can remove this valueerror: cannot convert float nan to integer error.

What is ValueError

Suppose you want to pass an input value as an argument to the functions. If the python interpreter finds that the input value is an invalid type then you will get the ValueError.

You can use the try and except to continue the code if you want to ignore this error.

What Causes valueerror: cannot convert float nan to integer

Now the question comes when you will get cannot convert float nan to integer error. Most of the time you will get this error when you are converting float columns to integers using the numpy astype() method. Also in that columns, there will be records that contain NaN values.

Let’s create this error by running the below lines of code.

import pandas as pd
import numpy as np
data = {"name":["Rob","Mona","Nikky","Bruno","Monica"],
        "weight":[56.5,np.nan,69.5,np.nan,76]}
df =pd.DataFrame(data)
df.weight.astype(int)
print(df)

Output

valueerror cannot convert float nan to integer error
valueerror cannot convert float nan to integer error

Solution for valueerror: cannot convert float nan to integer

There are many ways you can solve this valueerror. We will discuss each of them.

Solution 1: Remove Rows with NaN value

You already know there is no use to keep the rows with the NaN value if you are doing the pre-processing task for machine learning models. You can remove the rows containing the NaN value using the dropna() method.

Execute the below lines of code to remove NaN rows and remove this valueerror.

import pandas as pd
import pandas as pd
import numpy as np
data = {"name":["Rob","Mona","Nikky","Bruno","Monica"],
        "weight":[56.5,np.nan,69.5,np.nan,76]}
df =pd.DataFrame(data)
df = df.dropna()
df.weight = df.weight.astype(int)
print(df)
print(df.weight.dtype)

Output

Removing rows containing NaN value
Removing rows containing NaN value

You will only get rows that do not contain NaN values.

Solution 2: Replace NaN values with 0

The other method to remove this cannot convert float nan to integer error is replacing NaN values with 0. After that, you will be able to convert the float values to int without getting any error.

Run the below lines of code to replace NaN with 0.

import pandas as pd
import numpy as np
data = {"name":["Rob","Mona","Nikky","Bruno","Monica"],
        "weight":[56.5,np.nan,69.5,np.nan,76]}
df =pd.DataFrame(data)
df = df.fillna(0)
df.weight = df.weight.astype(int)
print(df)
print(df.weight.dtype)

Output

Removing rows containing NaN with o and converting float to int
Removing rows containing NaN with o and converting float to int

 

Conclusion

These are the ways to solve the issue of cannot converting float nan to integer error. You have either remove the NaN rows or replace them with 0. It is upon your choice which solution you want to choose.

I hope this tutorial has solved your queries on removing this ValueError. In case you have any doubt then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner