Pandas groupby agg std NaN

时间:2018-05-12 13:48:11

标签: python pandas std nan pandas-groupby

输入:

df['PopEst']
    .astype('float')
    .groupby(ContinentDict)
    .agg(['size','sum','mean','std']))

输出:

            size            sum                mean              std
Asia          5     2.898666e+09       5.797333e+08     6.790979e+08
Australia     1     2.331602e+07       2.331602e+07              NaN
Europe        6     4.579297e+08       7.632161e+07     3.464767e+07
North America 2     3.528552e+08       1.764276e+08     1.996696e+08
South America 1     2.059153e+08       2.059153e+08              NaN

std 列中的某些值原来是 NaN ,如果该组只有一行,但我认为这些值应为0,为什么会这样?

1 个答案:

答案 0 :(得分:0)

根据documentnp.std(..., ddof=1)默认将“自由度增量”设置为1。要解决问题,只需将np.std替换为lambda x: np.std(x, ddof=0),然后将{ {1}}将更改为NaN