Pandas dataframe groupby to calculate population standard deviation

后端 未结 2 1706
野趣味
野趣味 2021-02-20 15:02

I am trying to use groupby and np.std to calculate a standard deviation, but it seems to be calculating a sample standard deviation (with a degrees of freedom equal to 1).

2条回答
  •  爱一瞬间的悲伤
    2021-02-20 15:26

    For degree of freedom = 0

    (This means that bins with one number will end up with std=0 instead of NaN)

    import numpy as np
    
    
    def std(x): 
        return np.std(x)
    
    
    df.groupby('A').agg(['mean', 'max', std])
    

提交回复
热议问题