映射器中的 NaN - 名称 'nan' 未定义
NaN in mapper - name 'nan' is not defined
我做的如下
mapper = {'a': 'b', 'c': nan, 'd': 'e', nan : nan}
df['b'] = [ mapper[x] for x in df['a'] ]
df['b'].value_counts()
和
NameError Traceback (most recent call last)
<ipython-input-48-3862b2347ce7> in <module>()
NameError: name 'nan' is not defined
怎么了?是编码错误还是文件错误?
您没有定义变量 nan
是什么,所以 Python 引发了 NameError
。如果要检查数字是否为 NaN
(不是数字),请使用 math.isnan(x)
,其中 x
是浮点数。
Python 没有内置名称 nan
,也没有关键字。
看起来好像忘记导入了; numpy
定义了这样一个名字:
from numpy import nan
从本地名称 df
我推断你可能正在使用 pandas; pandas' 文档通常使用 np.nan
,其中 np
是用 import numpy as np
导入的 numpy
模块。例如,请参阅他们的 10 Minute to pandas intro。
我做的如下
mapper = {'a': 'b', 'c': nan, 'd': 'e', nan : nan}
df['b'] = [ mapper[x] for x in df['a'] ]
df['b'].value_counts()
和
NameError Traceback (most recent call last)
<ipython-input-48-3862b2347ce7> in <module>()
NameError: name 'nan' is not defined
怎么了?是编码错误还是文件错误?
您没有定义变量 nan
是什么,所以 Python 引发了 NameError
。如果要检查数字是否为 NaN
(不是数字),请使用 math.isnan(x)
,其中 x
是浮点数。
Python 没有内置名称 nan
,也没有关键字。
看起来好像忘记导入了; numpy
定义了这样一个名字:
from numpy import nan
从本地名称 df
我推断你可能正在使用 pandas; pandas' 文档通常使用 np.nan
,其中 np
是用 import numpy as np
导入的 numpy
模块。例如,请参阅他们的 10 Minute to pandas intro。