pandas.value_counts 不适用

sds*_*sds 5 python types pandas

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
Run Code Online (Sandbox Code Playgroud)

但不适用于字符串:

> 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'>]
Run Code Online (Sandbox Code Playgroud)

这里怎么会有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'>
Run Code Online (Sandbox Code Playgroud)

如何访问Nonein的计数s

Diz*_*ahi 0

我也很惊讶地发现这cv[np.nan]不起作用,但它确实有效:vc.loc[np.nan]