2

I want to normalize the column in the following dataframe:

import pandas as pd
from pprint import pprint
d = {'A': [1,0,3,0], 'B':[2,0,1,0], 'C':[0,0,8,0], 'D':[1,0,0,1]}
df = pd.DataFrame(data=d)
df = (df - df.mean())/df.std()

I am not sure if the normalization is done row-wise or column-wise.

I intend to do (x - mean of elements in the column)/ standard deviation, for each column.

Is it required to divide the standard deviation by number of entries in each column?

Massifox
  • 4,369
  • 11
  • 31
Natasha
  • 1,111
  • 5
  • 28
  • 66
  • 1
    Can you post the expected output? – moys Sep 18 '19 at 07:44
  • _to divide the standard deviation by number of entries_: If you are asking about normalization by degree of freedom, `pandas` already does it for you with `N-ddof`, where `ddof == 1` by default. – Chris Sep 18 '19 at 07:46

2 Answers2

3

Your code is run column-wise and it works correctly. However, if this was your question, there are other types of normalization, here are some that you might need:

Mean normalization (like you did):

normalized_df=(df-df.mean())/df.std()
          A         B    C         D
0  0.000000  1.305582 -0.5  0.866025
1 -0.707107 -0.783349 -0.5 -0.866025
2  1.414214  0.261116  1.5 -0.866025
3 -0.707107 -0.783349 -0.5  0.866025

Min-Max normalization:

normalized_df=(df-df.min())/(df.max()-df.min())
          A    B    C    D
0  0.333333  1.0  0.0  1.0
1  0.000000  0.0  0.0  0.0
2  1.000000  0.5  1.0  0.0
3  0.000000  0.0  0.0  1.0

Using sklearn.preprocessin you find a lot of normalization methods (and not only) ready, such as StandardScaler, MinMaxScaler or MaxAbsScaler:

Mean normalization using sklearn:

import pandas as pd
from sklearn import preprocessing

mean_scaler = preprocessing.StandardScaler(copy=True, with_mean=True, with_std=True)
x_scaled = mean_scaler.fit_transform(df.values)
normalized_df = pd.DataFrame(x_scaled)

          0         1         2    3
0  0.000000  1.507557 -0.577350  1.0
1 -0.816497 -0.904534 -0.577350 -1.0
2  1.632993  0.301511  1.732051 -1.0
3 -0.816497 -0.904534 -0.577350  1.0

Min-Max normalization using sklearn MinMaxScaler:

import pandas as pd
from sklearn import preprocessing

min_max_scaler = preprocessing.MinMaxScaler()
x_scaled = min_max_scaler.fit_transform(df.values)
normalized_df = pd.DataFrame(x_scaled)

          0    1    2    3
0  0.333333  1.0  0.0  1.0
1  0.000000  0.0  0.0  0.0
2  1.000000  0.5  1.0  0.0
3  0.000000  0.0  0.0  1.0

I hope I have helped you!

Massifox
  • 4,369
  • 11
  • 31
2

Your formula is run column wise & the result is as below.

          A            B       C    D
0   0.000000    1.305582    -0.5    0.866025
1   -0.707107   -0.783349   -0.5    -0.866025
2   1.414214    0.261116    1.5     -0.866025
3   -0.707107   -0.783349   -0.5    0.866025

You can double check with the code below on your df.

for col in df.columns:
    df[col+'_mean']= df[col].mean()
    df[col+'_std']= df[col].std()

This will give the output as below & you can use that to verify if you are getting what you need.

    A   B   C   D   A_mean  A_std   B_mean  B_std   C_mean  C_std   D_mean  D_std
0   1   2   0   1   1.0     1.414214    0.75    0.957427    2.0     4.0     0.5     0.57735
1   0   0   0   0   1.0     1.414214    0.75    0.957427    2.0     4.0     0.5     0.57735
2   3   1   8   0   1.0     1.414214    0.75    0.957427    2.0     4.0     0.5     0.57735
3   0   0   0   1   1.0     1.414214    0.75    0.957427    2.0     4.0     0.5     0.57735
moys
  • 7,747
  • 2
  • 11
  • 42