Home > Blockchain >  norm for all columns in a pandas datafrme
norm for all columns in a pandas datafrme

Time:05-17

With a dataframe like this:

index  col_1   col_2   ...   col_n
0      0.2     0.1           0.3
1      0.2     0.1           0.3
2      0.2     0.1           0.3
...
n      0.4     0.7           0.1


How can one get the norm for each column ? Where the norm is the sqrt of the sum of the squares.

I am able to do this for each column sequentially, but am unsure how to vectorize (avoiding a for loop) the same to an answer:

import pandas as pd
import numpy as np


norm_col_1 = np.linalg.norm(df[col_1])
norm_col_2 = np.linalg.norm(df[col_2])

norm_col_n = np.linalg.norm(df[col_n])

the answer would be a new dataframe series like this:

        norms
col_1   0.111
col_2   0.202
col_3   0.55
...
con_n   0.100

CodePudding user response:

You can pass the entire DataFrame to np.linalg.norm, along with an axis argument of 0 to tell it to apply it column-wise:

np.linalg.norm(df, axis=0)

To create a series with appropriate column names, try:

results = pd.Series(data=np.linalg.norm(df, axis=0), index=df.columns)
  • Related