How to do vector Normalization on columns using python function

Question:

The below table having 1000 rows but here let’s consider 3 rows:

Date B C
2022-07-24 100 1234
2021-02-01 200 6789
2020-04-30 300 4324

enter image description here

where m is the number of rows in the dataset and n is the number of columns. i varies along rows and j varies along the column.

For each row of Column B and C, the formula I tried applying is:

df['B'] = df1['B']  / np.sqrt((df['B'].pow(2)).sum())
df['C'] = df1['C']  / np.sqrt((df['C'].pow(2)).sum())

I want to write the same code using Python.

Asked By: Madness

||

Answers:

you didn’t specified which cell you want to work on if you need the complete column u have to use Sum fun

Answered By: Mohamed Hassan

If need processing mupliple columns by list use:

cols = ['B','C']
df1[cols] = df1[cols]  / np.sqrt((df1[cols].pow(2)).sum())
print (df1)
         Date         B         C
0  2022-07-24  0.267261  0.151539
1  2021-02-01  0.534522  0.833711
2  2020-04-30  0.801784  0.531001
Answered By: jezrael
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.