Standard deviation Function in python pandas is used to calculate standard deviation of a given set of numbers, Standard deviation of a data frame, Standard deviation of column or column wise standard deviation in pandas and Standard deviation of rows, let’s see an example of each. We need to use the package name “statistics” in calculation of median. In this tutorial we will learn,
- How to find the standard deviation of a given set of numbers
- How to find standard deviation of a dataframe in pandas
- How to find the standard deviation of a column in pandas dataframe
- How to find row wise standard deviation of a pandas dataframe
Syntax of standard deviation Function in python
Parameters :
axis : {rows (0), columns (1)}
skipna : Exclude NA/null values when computing the result
level : If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a Series
ddof : Delta Degrees of Freedom. The divisor used in calculations is N – ddof, where N represents the number of elements.
numeric_only : Include only float, int, boolean columns. If None, will attempt to use everything, then use only numeric data. Not implemented for Series.
Standard deviation Function in Python pandas
Simple standard deviation function is shown below
# calculate standard deviation import numpy as np print(np.std([1,9,5,6,8,7])) print(np.std([4,-11,-5,16,5,7,9]))
output:
8.97881103594
Standard deviation of a dataframe in pandas python:
Create dataframe
import pandas as pd import numpy as np #Create a DataFrame d = { 'Name':['Alisa','Bobby','Cathrine','Madonna','Rocky','Sebastian','Jaqluine', 'Rahul','David','Andrew','Ajay','Teresa'], 'Score1':[62,47,55,74,31,77,85,63,42,32,71,57], 'Score2':[89,87,67,55,47,72,76,79,44,92,99,69], 'Score3':[56,86,77,45,73,62,74,89,71,67,97,68]} df = pd.DataFrame(d) df
So the resultant dataframe will be
Standard deviation of the dataframe in pandas python:
# standard deviation of the dataframe df.std()
will calculate the standard deviation of the dataframe across columns so the output will
Score1 17.446021
Score2 17.653225
Score3 14.355603
dtype: float64
Column wise Standard deviation of the dataframe in pandas python:
# column standard deviation of the dataframe df.std(axis=0)
axis=0 argument calculates the column wise standard deviation of the dataframe so the result will be
Score1 17.446021
Score2 17.653225
Score3 14.355603
dtype: float64
Row standard deviation of the dataframe in pandas python:
# Row standard deviation of the dataframe df.std(axis=1)
axis=1 argument calculates the row wise standard deviation of the dataframe so the result will be
Calculate the standard deviation of the specific Column in pandas python
# standard deviation of the specific column df.loc[:,"Score1"].std()
The above code calculates the standard deviation of the “Score1” column so the result will be