How to calculate the standard deviation using Pandas

Standard deviation is the measure of how spread out numbers are. Pandas is a library in Python that is used to calculate the ​standard deviation.

svg viewer

How it works

Standard deviation is calculated using the function .std(). However, the Pandas library creates the Dataframe object and then the function .std() is applied on that Dataframe.

Code

The following code calculates the standard deviation of three columns (i.e., Score1, Score2, and Score3).

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)
answer= df.std()
print("The standard deviations of the 3 columns are:")
print (answer)

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved