【问题标题】:Count NaNs when unicode values present存在 unicode 值时计算 NaN
【发布时间】:2014-03-29 09:52:53
【问题描述】:

大家早上好,

我有一个包含多个系列的 pandas 数据框。对于数据框中的给定系列,数据类型是 unicode、NaN 和 int/float。我想确定系列中 NaN 的数量,但不能使用内置的 numpy.isnan 方法,因为它不能安全地将 unicode 数据转换为它可以解释的格式。我提出了一个解决方法,但我想知道是否有更好/更 Pythonic 的方式来完成这项任务。

提前致谢, 迈尔斯

import pandas as pd
import numpy as np

test = pd.Series(data = [NaN, 2, u'string'])
np.isnan(test).sum()
#Error

#Work around
test2 = [x for x in test if not(isinstance(x, unicode))]
numNaNs = np.isnan(test2).sum()

【问题讨论】:

    标签: python numpy pandas nan python-unicode


    【解决方案1】:

    使用pandas.isnull:

    In [24]: test = pd.Series(data = [NaN, 2, u'string'])
    
    In [25]: pd.isnull(test)
    Out[25]: 
    0     True
    1    False
    2    False
    dtype: bool
    

    但是请注意,pd.isnull 也将None 视为True

    In [28]: pd.isnull([NaN, 2, u'string', None])
    Out[28]: array([ True, False, False,  True], dtype=bool)
    

    【讨论】:

    • 谢谢,这正是我想要的。我非常感谢迅速而完整的回应。 (等 10 分钟后我会接受你的回答)
    猜你喜欢
    • 2014-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 2013-10-02
    • 1970-01-01
    • 2011-05-31
    • 2015-06-25
    相关资源
    最近更新 更多