Pandas Dataframe 分组和标准差
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/41014814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas Dataframe grouping and standard deviation
提问by H. Lewis
Given a pandas dataframe in the following format:
给定以下格式的 Pandas 数据框:
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?
我怎样才能找到每组的平均标准偏差?
For example, group by groupNo, find a standard deviation of the attributes in that group number, find a mean of them standard deviations
例如,group by groupNo,找到该组号中属性的标准差,找到它们的均值标准差
Any help would be great, H
任何帮助都会很棒,H
回答by jezrael
I think you need GroupBy.stdwith DataFrame.mean:
我认为你需要GroupBy.std有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
Or maybe need:
或者可能需要:
print (df.groupby('GroupNo').mean().std(axis=1))
GroupNo
1 1.453848
2 0.788480
3 0.535371
4 1.149420
dtype: float64

