• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / pandas Standard Deviation – Using std() to Find Standard Deviation

pandas Standard Deviation – Using std() to Find Standard Deviation

January 10, 2022 Leave a Comment

To find the standard deviation of a series or a column in a DataFrame in pandas, the easiest way is to use the pandas std() function.

df["Column1"].std() 

You can also use the numpy std() function, but be careful as the default algorithm is different than the default pandas std() algorithm.

np.std(df["Column1"]) #Different result from default pandas function
np.std(df["Column1"],ddof=1) #Same result as default pandas function

When doing data analysis, the ability to compute different summary statistics, such as the mean or median of a variable, is very useful to help us understand the data. One such summary statistic which can be useful is the standard deviation of a variable.

Finding the standard deviation of columns or a Series using pandas is easy. We can use the pandas std() function to find the standard deviation of a column of numbers.

Let’s say we have the following DataFrame.

df = pd.DataFrame({'Name': ['Jim', 'Sally', 'Bob', 'Sue', 'Jill', 'Larry'],
                   'Weight': [160.20, 160.20, 209.45, 150.35, 187.52, 187.52],
                   'Height': [50.10, 68.94, 71.42, 48.56, 59.37, 63.42] })

print(df)
# Output: 
    Name  Weight  Height
0    Jim  160.20   50.10
1  Sally  160.20   68.94
2    Bob  209.45   71.42
3    Sue  150.35   48.56
4   Jill  187.52   59.37
5  Larry  187.52   63.42

To get the standard deviation of the column “Height”, we can use the pandas std() function in the following Python code:

print(df["Height"].std())

# Output:
9.49495532726019

Calculating the Standard Deviation of a Series with numpy

We can also find the standard deviation of a series using the numpy std() function. Depending on the complexity of our code, it might be faster to use the numpy std() function.

Let’s say we have the same dataset as above.

To get the standard deviation of the column “Height”, we can use the numpy std() function in the following Python code.

print(np.std(df["Height"]))

# Output:
8.667668692073754

As you can verify for yourself, this is a different result from the pandas std() function. The reason for this is the default normalization method is different between pandas and numpy.

To get the same standard deviation using both numpy and pandas, you need to pass ‘ddof=1’ to the numpy std() function.

print(np.std(df["Height"]))
print(np.std(df["Height"],ddof=1))
print(df["Height"].std())

# Output:
8.667668692073754
9.49495532726019
9.49495532726019

As you can see above, we received the same result from the code when we pass ‘ddof=1’ to the numpy std() function.

Hopefully this article has been helpful for you to understand how to find the standard deviation of a variable within a column or Series using pandas.

Other Articles You'll Also Like:

  • 1.  pandas cumsum – Find Cumulative Sum of Series or DataFrame
  • 2.  Check if Variable is String in Python
  • 3.  Union of Lists in Python
  • 4.  Using Python to Get and Print First N Items in List
  • 5.  Using Python Selenium Webdriver to Refresh Page
  • 6.  Convert String to Float with float() in Python
  • 7.  Write Integer to File Using Python
  • 8.  How to Read Pickle File from AWS S3 Bucket Using Python
  • 9.  Using Python to Print Degree Symbol
  • 10.  Remove Substring from String in Python with replace()

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy