[英]Creating a Euclidian Distance Column in Pandas from Separate X and Y Columns
I have a pandas DataFrame of NBA basketball shooting statistics like the following.我有一个 NBA 篮球投篮统计数据的 pandas DataFrame,如下所示。 Each row refers to an individual shot taken during a game.每行是指在比赛期间拍摄的单个镜头。 X and Y refer to the respective horizontal and vertical distances from the net during each shot. X 和 Y 指的是每次击球期间到球网的各自水平和垂直距离。
| SHOOTER | X | Y | SCORE |
| -------- | ------ | -------- | ---------- |
| PlayerA | -3.8 | 5.7 | MADE |
| PlayerB | 0.7 | 5.9 | MADE |
| PlayerC | -1.5 | 4.1 | MISSED |
| PlayerA | 4.2 | 5.6 | MADE |
I want to create a new column in the DataFrame showing each shot's Euclidian Distance from the xy coordinate (0, 0), to determine the distance from the net at each shot.我想在 DataFrame 中创建一个新列,显示每个镜头与 xy 坐标 (0, 0) 的欧几里得距离,以确定每次镜头与网的距离。 How would I go about doing this?我该怎么做呢?
You could create a function that uses the distance formula which is pretty staright forward.您可以创建一个使用非常简单的距离公式的函数。 Or just use numpy
and the np.linalg.norm()
.或者只使用numpy
和np.linalg.norm()
。
import pandas as pd
import numpy as np
columns = ['SHOOTER','X','Y','SCORE']
data = [['PlayerA',-3.8,5.7,'MADE'],
['PlayerB',0.7,5.9,'MADE'],
['PlayerC',-1.5,4.1,'MISSED'],
['PlayerA',4.2,5.6,'MADE']]
df = pd.DataFrame(data=data, columns=columns)
df['dist'] = np.linalg.norm(df.loc[:, ['X','Y']].values, axis=1)
Output:输出:
print(df)
SHOOTER X Y SCORE dist
0 PlayerA -3.8 5.7 MADE 6.850547
1 PlayerB 0.7 5.9 MADE 5.941380
2 PlayerC -1.5 4.1 MISSED 4.365776
3 PlayerA 4.2 5.6 MADE 7.000000
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.