Norm along the line in pandas

I have a pandas Dataframe with N columns representing the coordinates of a vector (e.g. X, Y, Z, but maybe more than three-dimensional).

I would like to aggregate a data frame along rows with an arbitrary function that combines the columns, for example, the norm: (X^2 + Y^2 + Y^2) .

I want to do something similar to what has been done here and here and here , but I want to keep it common enough that the number of columns can change, and it behaves like

 DataFrame.mean(axis = 1) 

or

 DataFrame.sum(axis = 1) 
+8
python pandas
source share
5 answers

I found a faster solution than @elyase suggested:

 np.sqrt(np.square(df).sum(axis=1)) 
+14
source share

Numpy provides the norm ... Usage:

 np.linalg.norm(df[['X','Y','Z']].values,axis=1) 
+5
source share

filter columns by name

 cols = ['X','Y','Z'] df[cols].mean(axis=1) df[cols].sum(axis=1) df[cols].apply(lambda values: sum([v**2 for v in values]), axis=1) 
+2
source share

You are looking for apply . Your example would look like this:

 >> df = pd.DataFrame([[1, 1, 0], [1, 0, 0]], columns=['X', 'Y', 'Z']) XYZ 0 1 1 0 1 1 0 0 >>> df.apply(lambda x: np.sqrt(x.dot(x)), axis=1) 0 1.414214 1 1.000000 dtype: float64 

This works for any number of measurements.

+2
source share

One line using any function of your choice (including lambda functions), e.g.

df.apply(np.linalg.norm, axis=1)

or

df.apply(lambda x: (x**2).sum()**.5, axis=1)

0
source share

All Articles