Pandas Dataframe grouping and standard deviation

508
December 15, 2016, at 12:44 PM

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?

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

Any help would be great, H

Answer 1

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

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
READ ALSO
Python - Printing out all data reveived from a socket

Python - Printing out all data reveived from a socket

I found come old TCP reverse shell made in python online and im editing it to fix it up, when it prints the output of the command it doesnt print it all if the command is too bigThis is the code for sending the commands and then printing the output

295
Effectively turning strings into unicode for python 2.7

Effectively turning strings into unicode for python 2.7

I'm following a turtorial on LDA and encountering a problem since the turtorial is made in python 3 and I'm working in 27 (the turtorial claims to work in both)

420
how can i create email template using Xml Odoo

how can i create email template using Xml Odoo

how can i create email template using Xml Odoo

417
strange behavior when trying to swap two variables in python

strange behavior when trying to swap two variables in python

I encounter the following code in how to think like a computer scientist, and it's a bit strange to meCould anyone explain to me what is going on? Here is how I tried to understand it

354