# Standard Deviation in Python (5 Examples)

In this post, I’ll illustrate how to **calculate the standard deviation** in Python.

The page is structured as follows:

Here’s the step-by-step process…

## Example 1: Standard Deviation of List Object

This example illustrates how to get the standard deviation of a list object.

First, we have to create an example list:

my_list = [2, 7, 5, 5, 3, 9, 5, 9, 3, 1, 1] # Create example list print(my_list) # Print example list # [2, 7, 5, 5, 3, 9, 5, 9, 3, 1, 1] |

my_list = [2, 7, 5, 5, 3, 9, 5, 9, 3, 1, 1] # Create example list print(my_list) # Print example list # [2, 7, 5, 5, 3, 9, 5, 9, 3, 1, 1]

Then, we also have to import the NumPy library:

import numpy as np # Load NumPy library |

import numpy as np # Load NumPy library

Now, we can apply the std function of the NumPy library to our list to return the standard deviation:

print(np.std(my_list)) # Get standard deviation of list # 2.7423823870906103 |

print(np.std(my_list)) # Get standard deviation of list # 2.7423823870906103

The previous output shows the standard deviation of our list, i.e. 2.74.

Please note that this result reflects the population standard deviation. You may calculate the sample standard deviation by specifying the ddof argument within the std function to be equal to 1.

## Example 2: Standard Deviation of One Particular Column in pandas DataFrame

In this example, I’ll illustrate how to compute the standard deviation for one single column of a pandas DataFrame.

As a first step, we have to load the pandas library:

import pandas as pd # Import pandas library in Python |

import pandas as pd # Import pandas library in Python

Furthermore, we have to create an exemplifying pandas DataFrame:

data = pd.DataFrame({'x1':range(42, 11, - 2), # Create pandas DataFrame 'x2':[5, 9, 7, 3, 1, 4, 5, 4, 1, 2, 3, 3, 8, 1, 7, 5], 'x3':range(200, 216), 'group':['A', 'C', 'B', 'C', 'B', 'B', 'C', 'A', 'C', 'A', 'C', 'A', 'B', 'C', 'B', 'B']}) print(data) # Print pandas DataFrame |

data = pd.DataFrame({'x1':range(42, 11, - 2), # Create pandas DataFrame 'x2':[5, 9, 7, 3, 1, 4, 5, 4, 1, 2, 3, 3, 8, 1, 7, 5], 'x3':range(200, 216), 'group':['A', 'C', 'B', 'C', 'B', 'B', 'C', 'A', 'C', 'A', 'C', 'A', 'B', 'C', 'B', 'B']}) print(data) # Print pandas DataFrame

Table 1 shows the output of the previously shown Python programming code – A pandas DataFrame with four columns.

In the next step, we can apply the std function to a specific variable (i.e. x1) of our data set:

print(data['x1'].std()) # Get standard deviation of one column # 9.521904571390467 |

print(data['x1'].std()) # Get standard deviation of one column # 9.521904571390467

After executing the previous Python syntax, the console returns our result, i.e. a standard deviation of 9.52.

## Example 3: Standard Deviation of All Columns in pandas DataFrame

In this section, I’ll explain how to find the standard deviation for all columns of a pandas DataFrame.

Have a look at the following Python code:

print(data.std(axis = 1)) # Get standard deviation of rows # x1 9.521905 # x2 2.516611 # x3 4.760952 # dtype: float64 |

print(data.std(axis = 1)) # Get standard deviation of rows # x1 9.521905 # x2 2.516611 # x3 4.760952 # dtype: float64

As you can see, the previous Python code has returned a standard deviation value for each of our float columns.

## Example 4: Standard Deviation of Rows in pandas DataFrame

In this example, I’ll illustrate how to compute the standard deviation for each of the rows in a pandas DataFrame.

In order to do this, we have to specify axis equal to 1 within the std function:

print(data.std(axis = 1, numeric_only = True)) # Get standard deviation of rows # 0 103.568013 # 1 103.074407 # 2 104.787086 # 3 107.220956 # 4 108.932701 # 5 108.868422 # 6 109.546033 # 7 110.924900 # 8 112.988200 # 9 113.694034 # 10 114.421735 # 11 115.494589 # 12 115.001449 # 13 118.306100 # 14 117.542900 # 15 119.274194 # dtype: float64 |

print(data.std(axis = 1, numeric_only = True)) # Get standard deviation of rows # 0 103.568013 # 1 103.074407 # 2 104.787086 # 3 107.220956 # 4 108.932701 # 5 108.868422 # 6 109.546033 # 7 110.924900 # 8 112.988200 # 9 113.694034 # 10 114.421735 # 11 115.494589 # 12 115.001449 # 13 118.306100 # 14 117.542900 # 15 119.274194 # dtype: float64

The previous output shows a standard deviation for each row in our data matrix.

## Example 5: Standard Deviation by Group in pandas DataFrame

In Example 5, I’ll illustrate how to calculate the standard deviation for each group in a pandas DataFrame.

To accomplish this, we have to use the groupby function in addition to the std function:

print(data.groupby('group').std()) # Get standard deviation by group # x1 x2 x3 # group # A 9.574271 1.290994 4.787136 # B 11.290114 2.581989 5.645057 # C 8.891944 3.011091 4.445972 |

print(data.groupby('group').std()) # Get standard deviation by group # x1 x2 x3 # group # A 9.574271 1.290994 4.787136 # B 11.290114 2.581989 5.645057 # C 8.891944 3.011091 4.445972

As you can see, we have returned a separate standard deviation number for each of the groups in each of the variables of our pandas DataFrame.

## Video & Further Resources

Would you like to learn more about the calculation of the standard deviation? Then I recommend watching the following video on my YouTube channel. I explain the Python code of this tutorial in the video.

**Please accept YouTube cookies to play this video.** By accepting you will be accessing content from YouTube, a service provided by an external third party.

If you accept this notice, your choice will be saved and the page will refresh.

In addition, you may want to have a look at some of the related articles on my website. You can find a selection of articles that are related to the calculation of the standard deviation below.

- Standard Deviation by Group
- Standard Deviation of NumPy Array
- stdev & pstdev Functions of statistics Module
- Variance in Python
- pandas Library Tutorial in Python
- The Python Programming Language

This article has demonstrated how to **find the standard deviation** in the Python programming language. If you have additional questions, don’t hesitate to let me know in the comments section.