Hey, readers! In this article, we will be focusing on the 3 variants of standard deviation in Python.
So before getting started, let us first understand what’s Standard Deviation?
Standard deviation represents the deviation of the data values or entities with respect to the mean or the center value. It is mostly used in the domain of data analytics to explore and analyze the data distribution.
Now, let us further have a look at the various ways of calculating standard deviation in Python in the upcoming section.
Variant 1: Standard Deviation in Python using the stdev() function
Python statistics module provides us with
statistics.stdev() function to calculate the standard deviation of a set of values altogether.
In the below example, we have created a list and performed the standard deviation operation on the data values as shown below–
import statistics as std lst = [1,2,3,4,5] stat = std.stdev(lst) print(stat)
Variant 2: Standard deviation using NumPy module
NumPy module offers us various functions to deal with and manipulate the numeric data values.
We can calculate the standard deviation for the range of values using
numpy.std() function as shown below
import numpy as np num = np.arange(1,6) stat = np.std(num) print(stat)
Here, we have made use of
numpy.arange() function to generate a set of continuous values between 1-6. Further, the standard deviation has been calculated using std() function.
Variant 3: Standard deviation with Pandas module
Pandas module enables us to deal with a larger amount of datasets and also provides us with various functions to be performed on these datasets.
With the Pandas module, we can perform various statistics operations on the data values, one of them being standard deviation as shown below–
import pandas as pd lst = [1,2,3,4,5,6,7] data = pd.DataFrame(lst) stat = data.std() print(stat)
In this example, we have created a list and then converted the list into a data frame using pandas.dataframe() function. Further, we have calculated the standard deviation of those values present in the data frame using
0 2.160247 dtype: float64
By this, we have come to the end of this topic. Feel free to comment below in case you come across any questions.
For more such posts related to Python, stay tuned @ AskPython and Keep Learning!