Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Groupby数据帧手动计算STD_Python_Algorithm_Pandas - Fatal编程技术网

Python 使用Groupby数据帧手动计算STD

Python 使用Groupby数据帧手动计算STD,python,algorithm,pandas,Python,Algorithm,Pandas,我试图通过提供一种不同的手动方法来计算平均值和标准差来为编写一个解决方案 我创造了 然后,我创建了一个a的列表,没有重复。然后我检查了这些项目,每次对这些项目进行分组并计算出解决方案 import numpy as np l= list(set(df.A)) df.groupby('A', as_index=False) listMean=[0]*len(df.C) listSTD=[0]*len(df.C) for x in l: s= np.mean(df[df['A']==x

我试图通过提供一种不同的手动方法来计算平均值和标准差来为编写一个解决方案

我创造了

然后,我创建了一个a的列表,没有重复。然后我检查了这些项目,每次对这些项目进行分组并计算出解决方案

import numpy as np

l= list(set(df.A))

df.groupby('A', as_index=False)
listMean=[0]*len(df.C)
listSTD=[0]*len(df.C)

for x in l:
    s= np.mean(df[df['A']==x].C.values)
    z= [index for index, item in enumerate(df['A'].values) if x==item ]
    for i in z:
        listMean[i]=s

for x in l:
    s=  np.std(df[df['A']==x].C.values)
    z= [index for index, item in enumerate(df['A'].values) if x==item ]
    for i in z:
        listSTD[i]=s

df['C']= listMean
df['E']= listSTD

print df
我用“A”分组的
descripe()
来计算平均值std

print df.groupby('A').describe()
并测试了建议的解决方案:

result = df.groupby(['a'], as_index=False).agg(
                      {'c':['mean','std'],'b':'first', 'd':'first'})
我注意到当我计算std(“E”)时得到了不同的结果。我只是好奇,我错过了什么?

有:总体标准差和样本标准差

人口统计司

当这些值代表您正在研究的整个价值观时,使用

样本SD

当值只是来自该宇宙的样本时使用

默认情况下,
np.std
计算总体SD,而Pandas的
Series.std
计算样本SD

In [42]: np.std([4,5])
Out[42]: 0.5

In [43]: np.std([4,5], ddof=0)
Out[43]: 0.5

In [44]: np.std([4,5], ddof=1)
Out[44]: 0.70710678118654757

In [45]: x = pd.Series([4,5])

In [46]: x.std()
Out[46]: 0.70710678118654757

In [47]: x.std(ddof=0)
Out[47]: 0.5
ddof
代表“自由度”,控制从SD公式中的
N
中减去的数字

上面的公式图像来自。这里的“未修正样本标准偏差”是我(和)称之为总体SD,“修正样本标准偏差”是样本SD

In [42]: np.std([4,5])
Out[42]: 0.5

In [43]: np.std([4,5], ddof=0)
Out[43]: 0.5

In [44]: np.std([4,5], ddof=1)
Out[44]: 0.70710678118654757

In [45]: x = pd.Series([4,5])

In [46]: x.std()
Out[46]: 0.70710678118654757

In [47]: x.std(ddof=0)
Out[47]: 0.5