简体   繁体   English

分别规范化每一行

[英]Normalize each row separately

I would like to apply the normalization function of (first value - the value)/(standard deviation of each row) to each row separately.我想将(第一个值 - 值)/(每行的标准偏差)的标准化 function 分别应用于每一行。

For example:例如:

ID ID A一个 B C C
1 1 20 20 3 3 6 6
2 2 30 30 4 4 7 7
3 3 40 40 5 5 8 8
... ... ... ... ... ... ... ...
1000 1000 300 300 23 23 21 21

so I expect the first value of column "A" in each row will be zero.所以我希望每行中“A”列的第一个值为零。 imagine the rows are more than 1K.想象这些行超过 1K。

I tried to use this code but does not work我尝试使用此代码但不起作用

for i in range(len(df)):
new_norm = df((df.iloc[i][0] - df.iloc[i,:])/df.std(axis=1))
df= df.apply(new_norm, axis=1)
df

I used this one also and the same error我也用过这个,同样的错误

df = df((df.iloc[:,0] - df.iloc[:,:]) / df.std(axis=1))
import pandas as pd
df = pd.DataFrame({
    "ID": [1,2,3,10],    
    "A": [20,30,40,300],
    "B": [3,4,5,23],
    "C": [6,7,8,21]
})
vals = df.drop(columns=["ID"]).values
pd.DataFrame((vals[0, :] - vals) / vals.std(axis=0), columns=df.columns[1:])

output: output:

    A           B           C 
0   0.000000    0.000000    0.000000
1   -0.085377   -0.121101   -0.163846
2   -0.170755   -0.242202   -0.327693
3   -2.390566   -2.422019   -2.457696

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM