Pandas How To Uncategorized How to calculate z score

How to calculate z score

Here’s a code snippet to calculate the z-score in pandas:

import pandas as pd
from scipy.stats import zscore

df = pd.DataFrame({'col1': [1, 2, 3, 4, 5], 'col2': [1, 2, 3, 4, 5]})

z = df.apply(zscore)

The zscore function from the scipy.stats library is used to calculate the z-score, and the apply method of the DataFrame is used to apply the function to each column. The result is a new DataFrame where each column contains the z-scores for the corresponding column in the original DataFrame.

See also:
How to calculate percent change
How to calculate standard error in Pandas
How to calculate standard deviation in Pandas
How to calculate variance in Pandas


1 thought on “How to calculate z score”

Leave a Reply

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

Related Post