📌  相关文章
📜  如何修复:ValueError:无法将浮点 NaN 转换为整数

📅  最后修改于: 2022-05-13 01:55:19.474000             🧑  作者: Mango

如何修复:ValueError:无法将浮点 NaN 转换为整数

在本文中,我们将讨论如何修复值错误 - 在Python中无法将浮点 NaN 转换为整数。

在Python中,NaN 代表 Not a Number。当我们将包含 NaN 值的浮点类型的数据框列转换为整数时,将发生此错误。

让我们看看错误并探索处理它的方法。

正在使用的数据集:

让我们检查从浮点类型(标记列)转换为整数类型时的错误。我们可以使用 astype()函数进行转换

示例:描述错误

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)


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


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


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


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


输出:

因为 NaN 值不可能转换数据帧。所以为了解决这个问题,我们必须删除 NaN 值

方法 1:删除具有 NaN 值的行

在这里,我们将使用 dropna()函数从数据框列中删除 NaN 值。此函数将删除包含 NaN 值的行。

语法

dataframe.dropna()

示例:处理错误

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

输出:

方法 2:将 NaN 值替换为 0

我们可以将 NaN 值替换为 0 以摆脱 NaN 值。这是通过使用 fillna()函数来完成的。此函数将检查数据框列中的 NaN 值并填充给定值。

语法

dataframe.fillna(0)

示例:处理错误

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

输出:

方法 3:使用 numpy.nan_to_num()

这里我们使用 NumPy 将 NaN 值转换为 0 数字。

语法

numpy.nan_to_num(numpy.nal)

示例:处理错误

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

输出:

nan
0.0

方法 4:使用 Nullable

我们可以将 nan 值创建为 NaN,这在将浮点数转换为整数时不会产生任何错误。

语法

numpy.NaN

示例:处理错误

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

输出:

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')