Homesand
Homesand

Reputation: 423

How to standardize rows in pandas?

I have a dataset like:

value_last_1    value_last_2    value_last_3    value_last_4
53.40           91.29           106.56          34.71
131.92          81.53           70.57           31.82
0.00            0.00            21.27           12.55

It's easy to standardize the data set by columns using sklearn.preprocessing.StandardScaler. However, if I want to standardize by rows how can I do that without transposing the dataset?

Upvotes: 1

Views: 3337

Answers (1)

BENY
BENY

Reputation: 323226

Yes you can with pandas mean and std

df.sub(df.mean(1), axis=0).div(df.std(1), axis=0)
Out[841]: 
   value_last_1  value_last_2  value_last_3  value_last_4
0     -0.545272      0.596815      1.057086     -1.108629
1      1.283973      0.062308     -0.203409     -1.142872
2     -0.813624     -0.813624      1.233186      0.394061

Upvotes: 2

Related Questions