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 as0or'index') or the column (designated as1orcolumns) axis.skipna: This takes a Boolean value indicating whether NA or null values are to be excluded.ddof: This takes anintthat 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 libraryimport pandas as pd# Creating a DataFramedf = pd.DataFrame([[1,2,3,4,5],[1,7,5,9,0.5],[3,11,13,14,12]],columns=list('ABCDE'))# Printing the DataFrameprint(df)# Obtaining the median value vertically across rowsprint(df.std())# Obtaining the median value horizontally over columnsprint(df.std(axis="columns"))
Explanation
- Line 4: We import the
pandaslibrary. - 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 (axis0). 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 (axis1). We print the result to the console.