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
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?
CodePudding user response:
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