Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

pandas

How to use the std() function in Pandas

Onyejiaku Theophilus Chidalu

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"))
Implementing the std() function

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.

RELATED TAGS

pandas

CONTRIBUTOR

Onyejiaku Theophilus Chidalu
RELATED COURSES

View all Courses

Keep Exploring