How to use the std() function in Pandas

Overview

The std() function in pandas obtains the standard deviation of the values of a specified axis of a given DataFrame.

Mathematically, the standard deviation is defined as measuring the dispersion of each value in a dataset from the mean.

Syntax

The std() function takes the syntax shown below:

DataFrame.std(axis=NoDefault.no_default, skipna=True, numeric_only=None, **kwargs)
Syntax for the std() function in Pandas

Parameter value

The std() function takes the following optional parameter values:

  • axis: This represents the name for the row (designated as 0 or 'index') or the column (designated as 1 or columns) axis.
  • skipna: This takes a Boolean value indicating whether NA or null values are to be excluded.
  • ddof: This takes an int that represents the delta degrees of freedom.
  • numeric_only: This takes a Boolean value indicating whether to include only float, int, or Boolean columns.
  • **kwargs: This is an additional keyword argument that can be passed to the function.

Example

# A code to illustrate the std() function in Pandas
# Importing the pandas library
import pandas as pd
# Creating a DataFrame
df = pd.DataFrame([[1,2,3,4,5],
[1,7,5,9,0.5],
[3,11,13,14,12]],
columns=list('ABCDE'))
# Printing the DataFrame
print(df)
# Obtaining the median value vertically across rows
print(df.std())
# Obtaining the median value horizontally over columns
print(df.std(axis="columns"))

Explanation

  • Line 4: We import the pandas library.
  • Lines 7–10: We create a DataFrame, df.
  • Line 12: We print  df.
  • Line 15: Using the std() function, we obtain the standard deviation of values that run downwards across the rows (axis 0). We print the result to the console.
  • Line 18: Using the std() function, we obtain the standard deviation of values that run horizontally across the columns (axis 1). We print the result to the console.
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources