Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to convert element to Z value using mean, std for each column?

I want to know how to convert each column’s value to some value using pandas and numpy.

import pandas as pd
import numpy as np

matrix = [[1,2,3], [4,5,6], [7,8,9]]

Original Matrix

a b c
1 2 3
4 5 6
7 8 9

Output Matrix

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

a b c
-1.22 2 3
0 5 6
1.22 8 9

For examples, column ‘a’ is [1, 4, 7].
‘a”s mean is 4 and ‘a”s std is 2.45
So, Output is [(1-4) / 2.45, (4-4) / 2.45, (7-4) / 2.45]

How to convert each values using pandas and numpy?

>Solution :

Use:

matrix = [[1,2,3], [4,5,6], [7,8,9]]
df = pd.DataFrame(matrix, columns=list('abc'))

If need processing all columns subtract all values by means and divide by DataFrame.std with set ddof=0, because default is ddof=1.

df = df.sub(df.mean()).div(df.std(ddof=0))
print (df)
          a         b         c
0 -1.224745 -1.224745 -1.224745
1  0.000000  0.000000  0.000000
2  1.224745  1.224745  1.224745

If need processing only one column:

df['a'] = df['a'].sub(df['a'].mean()).div(df['a'].std(ddof=0))
print (df)
          a  b  c
0 -1.224745  2  3
1  0.000000  5  6
2  1.224745  8  9
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading