NA的pandas.value_counts

时间:2016-09-08 13:16:32

标签: python pandas types

pandas.value_counts 适用于None的数字数组:

> s = pd.Series([1,2,1,None])
> vc = s.value_counts(dropna=False)
> vc
 1.0    2
 2.0    1
NaN     1
dtype: int64
> vc.index
Float64Index([1.0, 2.0, nan], dtype='float64')
> vc[1], vc[float('NaN')]
2 1

但不是字符串:

> s = pd.Series(['1','2','1',None])
> vc = s.value_counts(dropna=False)
> vc
1      2
2      1
NaN    1
dtype: int64
> vc.index
Index([u'1', u'2', nan], dtype='object')
> [type(o) for o in vc.index]
[<type 'str'>, <type 'str'>, <type 'float'>]

这里怎么会有float

> vc['1']
2
> vc[float('NaN')]
TypeError: cannot do label indexing on <class 'pandas.indexes.base.Index'> with these indexers [nan] of <type 'float'>

如何访问None中的s的计数?

1 个答案:

答案 0 :(得分:1)

我也很惊讶地看到vc.loc[np.nan]不起作用,但确实如此:{{1}}