简体   繁体   English

从单独的 X 和 Y 列在 Pandas 中创建欧几里得距离列

[英]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() .或者只使用numpynp.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.

相关问题 从欧几里得距离矩阵创建 3D 点云 - Creating a 3D point cloud from an euclidian Distance Matrix 熊猫-基于2列和一个单独的测试列创建2个新列 - Pandas - creating 2 new columns based on 2 columns and a separate test column 如何使用来自 pandas DataFrame 的两个单独列的数据在 python 中创建一个新列? - How to creating a new column in python using data from two separate columns of a pandas DataFrame? Pandas - 如果列 y 包含 b,则从列中获取值 x - Pandas - Grab value x from column a if column y contains b 从其他 pandas 列创建新列 - Creating New columns from other pandas column 用熊猫创建稀疏矩阵,并将.dat文件的一列中的值填充到.dat文件的其他两列中的索引[x,y]处 - Create sparse matrix with pandas and fill it with values from one column of .dat file at indexes [x,y] from other two columns of .dat file 列表的熊猫列以单独的列 - Pandas column of lists to separate columns 从 pandas 列中解压缩可变长度字典并创建单独的列 - unpack variable length dictionary from pandas column and create separate columns 将列中的字符串扩展为 Pandas 中的不同单独列 - Expand a string from a column into different separate columns in Pandas 从 pandas dataframe 列中的列表中分离 dict 到不同的 dataframe 列 - separate dict from list in pandas dataframe column into different dataframe columns
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM