# Calculate The Average, Variance, And Standard Deviation

## Goal¶

This post aims to introduce how to calculate the average, variance and standard deviation of matrix using pandas.

## Libraries¶

In [2]:
import pandas as pd
import numpy as np


## Create a matrix¶

In [13]:
n = 1000
df = pd.DataFrame({'rand': np.random.rand(n),
'randint': np.random.randint(low=0, high=100, size=n),
'randn': np.random.randn(n),
'random_sample': np.random.random_sample(size=n),
'binomial': np.random.binomial(n=1, p=.5, size=n),
'beta': np.random.beta(a=1, b=1, size=n),
})

Out[13]:
rand randint randn random_sample binomial beta
0 0.689690 59 0.416245 0.607567 1 0.532052
1 0.288356 2 0.092351 0.311634 0 0.192651
2 0.173002 50 -0.626691 0.920702 0 0.342812
3 0.953088 17 -0.149677 0.316060 1 0.792191
4 0.693120 94 0.264678 0.060313 1 0.059370

## Calculate average, variance, and standard deviation¶

### Calculate by each function¶

In [16]:
df.mean()

Out[16]:
rand              0.497015
randint          49.224000
randn            -0.054651
random_sample     0.504412
binomial          0.490000
beta              0.508469
dtype: float64
In [15]:
df.var()

Out[15]:
rand               0.083301
randint          791.485309
randn              1.033378
random_sample      0.081552
binomial           0.250150
beta               0.083489
dtype: float64
In [17]:
df.std()

Out[17]:
rand              0.288619
randint          28.133349
randn             1.016552
random_sample     0.285573
binomial          0.500150
beta              0.288944
dtype: float64

### Calculate using describe¶

In [18]:
df.describe()

Out[18]:
rand randint randn random_sample binomial beta
count 1000.000000 1000.000000 1000.000000 1000.000000 1000.00000 1000.000000
mean 0.497015 49.224000 -0.054651 0.504412 0.49000 0.508469
std 0.288619 28.133349 1.016552 0.285573 0.50015 0.288944
min 0.000525 0.000000 -3.405606 0.001359 0.00000 0.000373
25% 0.241000 25.000000 -0.741640 0.264121 0.00000 0.256070
50% 0.497571 48.000000 -0.074852 0.505738 0.00000 0.523674
75% 0.742702 73.000000 0.602928 0.743445 1.00000 0.758901
max 0.999275 99.000000 3.861652 0.995010 1.00000 0.999007