小编Myl*_*ker的帖子

当unicode值存在时计算NaNs

大家早上好,

我有一个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()
Run Code Online (Sandbox Code Playgroud)

python numpy nan pandas python-unicode

5
推荐指数
1
解决办法
1055
查看次数

标签 统计

nan ×1

numpy ×1

pandas ×1

python ×1

python-unicode ×1