Python nan to int

Как исправить: ValueError: невозможно преобразовать число с плавающей запятой NaN в целое число

Одна ошибка, с которой вы можете столкнуться при использовании pandas:

ValueError : cannot convert float NaN to integer 

Эта ошибка возникает, когда вы пытаетесь преобразовать столбец в кадре данных pandas из числа с плавающей запятой в целое число, но столбец содержит значения NaN.

В следующем примере показано, как исправить эту ошибку на практике.

Как воспроизвести ошибку

Предположим, мы создаем следующие Pandas DataFrame:

import pandas as pd import numpy as np #create DataFrame df = pd.DataFrame() #view DataFrame df points assists rebounds 0 25 5 11 1 12 7 NaN 2 15 7 10 3 14 9 6 4 19 12 5 5 23 9 NaN 6 25 9 9 7 29 4 12 

В настоящее время столбец «отскоки» имеет тип данных «плавающий».

#print data type of 'rebounds' column df['rebounds']. dtype dtype('float64') 

Предположим, мы пытаемся преобразовать столбец «отскоки» из числа с плавающей запятой в целое число:

#attempt to convert 'rebounds' column from float to integer df['rebounds'] = df['rebounds'].astype (int) ValueError : cannot convert float NaN to integer 

Мы получаем ValueError , потому что значения NaN в столбце «отскоков» не могут быть преобразованы в целые значения.

Читайте также:  User dir user home java

Как исправить ошибку

Способ исправить эту ошибку состоит в том, чтобы иметь дело со значениями NaN, прежде чем пытаться преобразовать столбец из числа с плавающей запятой в целое число.

Мы можем использовать следующий код, чтобы сначала определить строки, содержащие значения NaN:

#print rows in DataFrame that contain NaN in 'rebounds' column print(df[df['rebounds']. isnull ()]) points assists rebounds 1 12 7 NaN 5 23 9 NaN 

Затем мы можем либо удалить строки со значениями NaN, либо заменить значения NaN каким-либо другим значением перед преобразованием столбца из числа с плавающей запятой в целое число:

Метод 1: удаление строк со значениями NaN

#drop all rows with NaN values df = df.dropna () #convert 'rebounds' column from float to integer df['rebounds'] = df['rebounds'].astype (int) #view updated DataFrame df points assists rebounds 0 25 5 11 2 15 7 10 3 14 9 6 4 19 12 5 6 25 9 9 7 29 4 12 #view class of 'rebounds' column df['rebounds']. dtype dtype('int64') 

Способ 2: заменить значения NaN

#replace all NaN values with zeros df['rebounds'] = df['rebounds']. fillna ( 0 ) #convert 'rebounds' column from float to integer df['rebounds'] = df['rebounds'].astype (int) #view updated DataFrame df points assists rebounds 0 25 5 11 1 12 7 0 2 15 7 10 3 14 9 6 4 19 12 5 5 23 9 0 6 25 9 9 7 29 4 12 #view class of 'rebounds' column df['rebounds']. dtype dtype('int64') 

Обратите внимание, что оба метода позволяют избежать ошибки ValueError и успешно преобразовать столбец с плавающей запятой в столбец с целым числом.

Дополнительные ресурсы

В следующих руководствах объясняется, как исправить другие распространенные ошибки в Python:

Источник

How to Solve Python ValueError: cannot convert float nan to integer

NaN stands for Not a Number. You may encounter the error ValueError: cannot convert float NaN to integer when attempting to convert a column in a Pandas DataFrame from a float to an integer, and the column contains NaN values.

You can solve this error by either dropping the rows with the NaN values or replacing the NaN values with another value that you can convert to an integer.

This tutorial will go through how to resolve the error with examples.

Table of contents

ValueError: cannot convert float nan to integer

What is a ValueError?

In Python, a value is the information stored within a certain object. You will encounter a ValueError in Python when you use a built-in operation or function that receives an argument that has the right type but an inappropriate value. Let’s look at an example of converting several a ValueError:

value = 'string' print(float(value))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) print(float(value)) ValueError: could not convert string to float: 'string'

The above code throws the ValueError because the value ‘ string ‘ is an inappropriate (non-convertible) string. You can only convert numerical strings using the float() method, for example:

The code does not throw an error because the float function can convert a numerical string. The value of 5 is appropriate for the float function.

What is a NaN?

In Python, a NaN stands for Not a Number and represents undefined entries and missing values in a dataset. NaN is a special floating-point value that cannot be converted to any other type than float. Therefore if we try to convert a NaN to an integer we will throw: ValueError: cannot convert float nan to integer.

Example: NaN Values in a DataFrame

You may encounter this ValueError when you attempt to convert a column in a pandas DataFrame from a float to an integer, yet the column contains NaN values. Let’s look at an example DataFrame that stores the exam results for three subjects: Physics, Chemistry, Biology. The results are on a scale of 0 to 100.

import pandas as pd import numpy as np df = pd.DataFrame(<'Physics':[50, 60, 70, 55, 47, 90], 'Chemistry':[70, 75, 55, 63, np.nan, 80], 'Biology':[80, np.nan, 55, 70, np.nan, 66]>) print(df)

In the above program, we import both pandas and numpy and create a DataFrame to store the exam results. We then print the DataFrame to the console. Let’s run the code to see the DataFrame:

Physics Chemistry Biology 0 50 70.0 80.0 1 60 75.0 NaN 2 70 55.0 55.0 3 55 63.0 70.0 4 47 NaN NaN 5 90 80.0 66.0

The columns Chemistry and Biology are of the data type float, which we can verify using dtype:

print(df['Physics'].dtype) print(df['Chemistry'].dtype) print(df['Biology'].dtype)

Let’s try to convert the Chemistry and Biology columns from float to integer:

df['Chemistry'] = df['Chemistry'].astype(int) df['Biology'] = df['Biology'].astype(int)
ValueError: Cannot convert non-finite values (NA or inf) to integer

The program throws the ValueError because the NaN values in the Chemistry and Biology columns cannot be converted to integer values.

Solution #1: Drop Rows with NaN Values Using dropna()

To solve this error we can remove the rows from the DataFrame that contains NaN values using the dropna() function. Let’s look at how to do this:

import pandas as pd import numpy as np df = pd.DataFrame(<'Physics':[50, 60, 70, 55, 47, 90], 'Chemistry':[70, 75, 55, 63, np.nan, 80], 'Biology':[80, np.nan, 55, 70, np.nan, 66]>) df = df.dropna() print(df) df['Chemistry'] = df['Chemistry'].astype(int) df['Biology'] = df['Biology'].astype(int) print(df) print(df['Chemistry'].dtype) print(df['Biology'].dtype)

The above program drops the rows that contain NaN values then converts each of the Chemistry and Biology columns to integer. The program prints the DataFrame after applying dropna() , after converting the columns and the data types of the Chemistry and Biology columns. Let’s run the program to get the output.

 Physics Chemistry Biology 0 50 70.0 80.0 2 70 55.0 55.0 3 55 63.0 70.0 5 90 80.0 66.0 Physics Chemistry Biology 0 50 70 80 2 70 55 55 3 55 63 70 5 90 80 66 int64 int64

Solution #2: Replacing NaN Values Using fillna()

Opting to remove rows that contain NaN values will result in losing important information. Instead of removing the rows, we can replace the NaN values with other values. In this example, we will replace the NaN values with zeros but they can be any other value. Let’s look at how to use the fillna() function:

import pandas as pd import numpy as np df = pd.DataFrame(<'Physics':[50, 60, 70, 55, 47, 90], 'Chemistry':[70, 75, 55, 63, np.nan, 80], 'Biology':[80, np.nan, 55, 70, np.nan, 66]>) df['Chemistry'] = df['Chemistry'].fillna(0) df['Biology'] = df['Biology'].fillna(0) df['Chemistry'] = df['Chemistry'].astype(int) df['Biology'] = df['Biology'].astype(int) print(df) print(df['Chemistry'].dtype) print(df['Biology'].dtype)

The above program returns:

 Physics Chemistry Biology 0 50 70 80 1 60 75 0 2 70 55 55 3 55 63 70 4 47 0 0 5 90 80 66 int64 int64

Both solutions allow us to convert the float columns to integer columns, but fillna() preserves values in the rows not containing NaN values.

Summary

Congratulations on reading to the end of this article! You will raise the error ValueError: cannot convert float nan to integer when you try to convert a NaN value to an integer. This commonly occurs when you try to convert a column in a DataFrame that contains NaN values from a float to an integer. You can solve this error by dropping the rows that contain the NaN values using dropna() or you can use fillna() to replace the NaN values with other values that you can convert to integers.

For the solution to another common ValueError involving NaN values, go to the article: How to Solve Python ValueError: input contains nan, infinity or a value too large for dtype(‘float64’)

Go to the Python online courses page to learn more about coding in Python for data science and machine learning.

Have fun and happy researching!

Share this:

Источник

valueerror: cannot convert float nan to integer ( Solved )

nxnxn matrix python Implementation Step by Step

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.

 df =pd.DataFrame(data) df.weight.astype(int) print(df)

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.

 df =pd.DataFrame(data) df = df.dropna() df.weight = df.weight.astype(int) print(df) print(df.weight.dtype)

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.

 df =pd.DataFrame(data) df = df.fillna(0) df.weight = df.weight.astype(int) print(df) print(df.weight.dtype)

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.

We respect your privacy and take protecting it seriously

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

Источник

Оцените статью