A Detailed Guide to the Python stdev() function With Examples

Hey, folks! In continuation of our series on Python statistical functions, today we will be unveiling standard deviation using the Python stdev() method.

Standard deviation is a statistical entity that represents the variation in the data i.e. it depicts the deviation of data values from the center value (the mean of the data).

Usually, standard deviation is calculated using the below formula–

Standard Deviation = (Variance)^1/2

Now, let us start with the implementation and calculation of Standard Deviation using Python in-built function.


Getting started with Python stdev() function

Python statistics module contains various in-built functions to perform the data analysis and other statistical functions. The statistics.stdev() function is used to calculate the standard deviation of the passed data values to the function as argument.

Syntax:

Example:

In the above example, we have created data of numbers from 1-10 using the range() function. Further, we apply the stdev() function to evaluate the standard deviation of the data values.

Output:


Python standard deviation with NumPy module

Python NumPy module converts the data elements into an array form to perform numeric manipulations on it.

Further, numpy.std() function can be used to calculate the standard deviation of all the data values present in the NumPy array.

Syntax:

We need to import the NumPy module into the Python environment to get access to the in-built functions of the same using the below code–

Example:

In the above example, we have generated an array of elements from 1-30 using numpy.arange() function. After which, we pass the array to the numpy.std() function to calculate the standard deviation of the array elements.

Output:


Python standard deviation with Pandas module

Python Pandas module converts the data values into a DataFrame and helps us analyse and work with huge datasets. The pandas.DataFrame.std() function is used to calculate the standard deviation of the data column values of a particular DataFrame.

Syntax:

Example 1:

In the above example, we have converted a NumPy array into a DataFrame and the applied the DataFrame.std() function to get the standard deviation of the data values.

Output:

Example 2:

In the above example, we have used a dataset and calculated the standard deviation of the data column ‘qsec’ using the DataFrame.std() function.

Input Dataset:

A Detailed Guide to the Python stdev() function

MTCARS Dataset

Output:


Conclusion

Thus, in this article, we have understood the working of Python stdev() function along with NumPy and Pandas module.


References

By admin

Leave a Reply