Pandas Dataframe 分组和标准差 [英] Pandas Dataframe grouping and standard deviation

查看:388
本文介绍了Pandas Dataframe 分组和标准差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定以下格式的 Pandas 数据框:

Given a pandas dataframe in the following format:

GroupNo | at1 | at2 | at3   | at4 
1       | 0.02|1.12 | 1.88  | 3.2
1       | 6.11|1.13 | 0.88  | 5.2
4       | 2.02|1.16 | 2.88  | 0.2
3       | 0.20|0.12 | 1.48  | 1.25
2       | 0.02|1.12 | 1.88  | 1.4
3       | 3.02|1.12 | 1.98  | 2.2
3       | 0.40|0.18 | 1.48  | 1.25

如何找到每组的平均标准差?

How could I find the mean standard deviation per group?

比如group by groupNo,求该组号中属性的一个标准差,求它们的均值标准差

For example, group by groupNo, find a standard deviation of the attributes in that group number, find a mean of them standard deviations

任何帮助都会很棒,高

推荐答案

我认为你需要 GroupBy.stdDataFrame.mean:

I think you need GroupBy.std with DataFrame.mean:

print (df.groupby('GroupNo').std())
              at1       at2       at3       at4
GroupNo                                        
1        4.306280  0.007071  0.707107  1.414214
2             NaN       NaN       NaN       NaN
3        1.573573  0.560833  0.288675  0.548483
4             NaN       NaN       NaN       NaN

print (df.groupby('GroupNo').std().mean(axis=1))
GroupNo
1    1.608668
2         NaN
3    0.742891
4         NaN
dtype: float64

或者可能需要:

print (df.groupby('GroupNo').mean().std(axis=1))
GroupNo
1    1.453848
2    0.788480
3    0.535371
4    1.149420
dtype: float64

这篇关于Pandas Dataframe 分组和标准差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆