问题:对pandas的DataFrame 或者Series 使用自带的.std()方法,出现nan值,为什么会出现?怎么最快的方式替换成0?
>>> a = pd.Series([1, 2])
Out[1]:
0 1
1 2
dtype: int64
>>> a.std()
Out[2]: 0.7071067811865476
>>> b = pd.Series([1])
Out[3]: array([1])
>>> b.std()
Out[4]: nan
>>> type(b.std())
Out[5]: float
解决:
(1)为什么会出现? 因为标准差的计算公式为:
当数据量为1时,pandas自带的.std()方法计算标准差时,不能计算,用float类型的nan来表示。
(2)怎么快速用零替代?将其转化为numpy.array形式
>>> c = np.array(pd.Series([1]))
Out[6]: array([1])
>>> c.std()
Out[7]: 0.0