I have a DataFrame which has 5 rows and 5 columns.
Created by the statement-
df = pd.DataFrame(np.random.randn(25).reshape(5,5), index=list('abcde'), columns=list('vwxyz'))
I NEED TO -
Define a function call 'Standardizing' which works on a series as:
Find the mean and standard deviation of the series
Subtract each value of the series with the mean
Divide the result with the standard deviation
Apply this function to each numerical column (int or float) of the DF.
To achieve this, I do -
1)
def Standardizing(y):
mean = y.mean()
sd = y.std()
return y.map(lambda x: round(((x-mean)/sd),2))
df.apply(Standardizing)
2)
def Standardizing2(y):
mean = y.mean()
sd = y.std()
return round((y-mean)/sd,2)
df.apply(Standardizing2)
While I understand, 1 should work. y is a Series for me and then I am using map to reach each element of Series.
Just of curiosity, I wrote 2 and that also works.
I do not understand why it works. I would really appreciate any inputs.