close

[Solved] ValueError: cannot convert float NaN to integer

Hello Guys, How are you all? Hope You all Are Fine. Today I am trying to read cvs with pandas But I am facing following error ValueError: cannot convert float NaN to integer in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How ValueError: cannot convert float NaN to integer Error Occurs?

I am trying to read cvs with pandas But I am facing following error.

ValueError: cannot convert float NaN to integer

How To Solve ValueError: cannot convert float NaN to integer Error ?

  1. How To Solve ValueError: cannot convert float NaN to integer Error ?

    To Solve ValueError: cannot convert float NaN to integer Error You have to convert int into float first. Here is my example. The size of the int if it's 32 or 64 depends on your variable, be aware you may loose some precision if your numbers are to big for the format. Second solution is You can use .loc to subset the dataframe by only values that are notnull(), and then subset out the 'x' column only. Take that same vector, and apply(int) to it.

  2. ValueError: cannot convert float NaN to integer

    To Solve ValueError: cannot convert float NaN to integer Error You have to convert int into float first. Here is my example. The size of the int if it's 32 or 64 depends on your variable, be aware you may loose some precision if your numbers are to big for the format. Second solution is You can use .loc to subset the dataframe by only values that are notnull(), and then subset out the 'x' column only. Take that same vector, and apply(int) to it.

Solution 1: convert int into float first

You have to convert int into float first. Here is my example. The size of the int if it’s 32 or 64 depends on your variable, be aware you may loose some precision if your numbers are to big for the format.

df['column_name'].astype(np.float).astype("Int32")

Solution 2: apply(int) to it

You can use .loc to subset the dataframe by only values that are notnull(), and then subset out the 'x' column only. Take that same vector, and apply(int) to it.

Here If column x is float:

df.loc[df['x'].notnull(), 'x'] = df.loc[df['x'].notnull(), 'x'].apply(int)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read