Python – Measuring Variance

Python – Measuring Variance

Python – Measuring Variance

In statistics, variance is a measure of how far a value in a data set lies from the mean value. In other words, it indicates how dispersed the values are.
It is measured by using standard deviation. The other method commonly used is skewness.
Both of these are calculated by using functions available in pandas library.

Measuring Standard Deviation

Standard deviation is square root of variance. variance is the average of squared difference of values in a data set from the mean value. In python we calculate this value
by using the function std() from pandas library.

import pandas as pd
#Create a Dictionary of series
data = {'Name':pd.Series(['Raj','Sham','Tusar','Ram','Jaggu','Karishma','Pranab',
'Shami','Chahal','Dhoni','Sachin','Bumrah']),,
'Age':pd.Series([21,22,24,28,19,18,20,32,32,35,42,29]),
'Rating':pd.Series([4.17,3.89,3.63,5.00,1.98,1.03,2.36,4.00,3.92,4.88,4.99,4.23])}
#Create a DataFrame
database = pd.DataFrame(database)
# Calculate the standard deviation
print database.std()

Its output is as follows −

Age       9.198657
Rating    1.789454
dtype: float64

Measuring Skewness

It used to determine whether the data is symmetric or skewed. If the index is between -1 and 1, then the distribution is symmetric. If the index is no more than -1
then it is skewed to the left and if it is at least 1, then it is skewed to the right

import pandas as pd
#Create a Dictionary of series
data = {'Name':pd.Series(['Raj','Sham','Tusar','Ram','Jaggu','Karishma','Pranab',
'Shami','Chahal','Dhoni','Sachin','Bumrah']),
'Age':pd.Series([21,22,24,28,19,18,20,32,32,35,42,29]),
'Rating':pd.Series([4.17,3.89,3.63,5.00,1.98,1.03,2.36,4.00,3.92,4.88,4.99,4.23])}
#Create a DataFrame
database = pd.DataFrame(data)
print database.skew()

Its output is as follows −

Age       2.569871
Rating   -1.796531
dtype: float64

So the distribution of age rating is symmetric while the distribution of age is skewed to the right.

Python – Binomial Distribution (Prev Lesson)
(Next Lesson) Python – Normal Distribution
', { 'anonymize_ip': true });