H. Lewis
H. Lewis

Reputation: 33

Pandas Dataframe grouping and standard deviation

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

Upvotes: 3

Views: 15694

Answers (1)

jezrael
jezrael

Reputation: 863611

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

Upvotes: 6

Related Questions