Norm along row in pandas
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18524112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Norm along row in pandas
提问by Fra
I have a pandas Dataframe with N columns representing the coordinates of a vector (for example X, Y, Z, but could be more than 3D).
I have a pandas Dataframe with N columns representing the coordinates of a vector (for example X, Y, Z, but could be more than 3D).
I would like to aggregate the dataframe along the rows with an arbitrary function that combines the columns, for example the norm: (X^2 + Y^2 + Y^2).
I would like to aggregate the dataframe along the 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 is done hereand hereand herebut I want to keep it general enough that the number of columns can change and it behaves like
I want to do something similar to what is done hereand hereand herebut I want to keep it general enough that the number of columns can change and it behaves like
DataFrame.mean(axis = 1)
or
or
DataFrame.sum(axis = 1)
回答by Fra
I found a faster solution than what @elyase suggested:
I found a faster solution than what @elyase suggested:
np.sqrt(np.square(df).sum(axis=1))
回答by ntg
Numpy provides norm... Use:
Numpy provides norm... Use:
np.linalg.norm(df[['X','Y','Z']].values,axis=1)
回答by PeterFoster
One line, using whatever function you desire (including lambda functions), e.g.
One line, using whatever function you desire (including lambda functions), e.g.
df.apply(np.linalg.norm, axis=1)
df.apply(np.linalg.norm, axis=1)
or
or
df.apply(lambda x: (x**2).sum()**.5, axis=1)
df.apply(lambda x: (x**2).sum()**.5, axis=1)
回答by mattexx
filter the columns by name
filter the 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)
回答by elyase
You are looking for apply. Your example would look like this:
You are looking for apply. Your example would look like this:
>> df = pd.DataFrame([[1, 1, 0], [1, 0, 0]], columns=['X', 'Y', 'Z'])
X Y Z
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 dimensions.
This works for any number of dimensions.

