Reputation: 2103
I'd like to calculate for each row of a dataframe the norm (i.e. the length of the vector in euclidean distance). I know functions like mean()
and sum()
exist but no norm()
. So I tried implementing it myself by doing
df.apply(lambda values: math.sqrt(sum([v**2 for v in values])), axis=1)
but this is realy slow compared to e.g. the sum function. Is there a simple (and fast) pandas implementation for it?
Upvotes: 1
Views: 782
Reputation: 96236
Sure, use numpy
here:
In [1]: import pandas as pd, numpy as np
In [2]: df = pd.DataFrame(data=[[1,2,3],[4,5,6]], columns=['a','b','c'])
In [3]: np.linalg.norm(df, axis=1)
Out[3]: array([3.74165739, 8.77496439])
Upvotes: 2