Standard deviation Function in Python pandas (Dataframe, Row and column wise standard deviation)

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 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
  • How to find the standard deviation of a column in dataframe
  • How to find row wise standard deviation of a dataframe

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:

2.82842712475
8.97881103594

 

Standard deviation of a dataframe:

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 Function in Python - pandas (Dataframe, Row and column wise standard deviation) 01

 

Standard deviation of the dataframe:


# 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 Standard deviation of the dataframe:


# 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:


# 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

0     17.578396
1     22.810816
2     11.015141
3     14.730920
4     21.197484
5     7.637626
6     5.859465
7     13.114877
8     16.196707
9     30.138569
10     15.620499
11     6.658328
dtype: float64

 

Calculate the standard deviation of the specific Column


# 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

17.446020645512156

 

previous-small standard deviation function in python pandas                                                                                                                next_small standard deviation function in python pandas