简体   繁体   English

Pandas:计算 X、Y 分组之间的距离和角度

[英]Pandas: Calculate Distance and Angle between X, Y groupped

Well, I have following columns:好吧,我有以下列:

Id PlayId  X     Y
0  0       2.3   3.4
1  0       5.4   3.2

2  1       3.2   5.1
3  1       4.2   1.7

If I have two rows groupped by one PlayId, I want to add two columns of Distance and Angle:如果我有两行按一个 PlayId 分组,我想添加两列距离和角度:

Id PlayId  X     Y   Distance_0  Distance_1 Angle_0 Angle_1
0  0       2.3   3.4 0.0         ?          0.0     ?
1  0       5.4   3.2 ?           0.0        ?       0.0

2  1       3.2   5.1
3  1       4.2   1.7

Every Distance-column describes Euclidean distance between i-th and j-th element in a group:每个距离列都描述了组中第 i 个和第 j 个元素之间的欧几里得距离:

dist(x0, x1, y0, y1) = sqrt((x0 - x1) ** 2 + (y0 - y1) ** 2)

Similar way, the angle between i-th and j-th element is calculated.类似地,计算第 i 个和第 j 个元素之间的角度。

So, how can I perform this efficiently, without processing elements one-by-one?那么,如何在不逐个处理元素的情况下有效地执行此操作?

You can compute the pairwise distances by using the pdist function from SciPy:您可以使用来自 SciPy 的pdist function 计算成对距离:

df = pd.DataFrame({'X': [5, 6, 7], 'Y': [3, 4, 5]})

# df
#    X  Y
# 0  5  3
# 1  6  4
# 2  7  5

from scipy.spatial.distance import pdist, squareform

cols = [f'Distance_{i}' for i in range(len(df))]
pd.DataFrame(squareform(pdist(df.values)), columns=cols)

which produces the following DataFrame:产生以下 DataFrame:

Distance_0  Distance_1  Distance_2
0   0.000000    1.638991    2.828427
1   1.638991    0.000000    1.638991
2   2.828427    1.638991    0.000000

This works, since pdist takes an array of size m * n, where m is the number of observations (=rows) and n the dimension of said observations (in this case: two - X and Y)这是可行的,因为pdist采用大小为 m * n 的数组,其中 m 是观察的数量(=行),n 是所述观察的维度(在这种情况下:两个 - X 和 Y)

You could subsequently concat the original DataFrame with the newly created one if needed (using pd.concat ).如果需要,您可以随后将原始 DataFrame 与新创建的连接(使用pd.concat )。

For the angle, you could use pdist as well, using metric='cosine' to compute the cosine distance.对于角度,您也可以使用pdist ,使用metric='cosine'来计算余弦距离。 See this post for more information.有关更多信息,请参阅此帖子

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何计算一组 x、y 坐标和位置变量之间的距离? - How do I calculate the distance between a set x, y coordinate and location variables? 计算两点之间的距离,如果 &lt;1 :添加到列表 x 和 y 坐标(人口程序)Python - calculate distance between two points, if <1 : add to lists x and y coords (population program) Python 如何计算熊猫和列表之间的欧几里德距离? - How calculate euclidean distance between pandas and list? Python - 计算从对象 1(作为 x 和 y)到对象 2(作为 x 和 y)的角度 - Python - Calculate angle from object1(as x and y) to object2(as x and y) 在 Python 中计算直线(斜率为 x)和水平之间的角度(度数) - Calculate angle (degrees) in Python between line (with slope x) and horizontal scipy成对距离与X.X + YY - XY ^ t之间的差异 - Difference between scipy pairwise distance and X.X+Y.Y - X.Y^t Python:使用到点 A 的距离(x0,y0)和角度找到给定点 B 的 ax,y 坐标 - Python: find a x,y coordinate for a given point B using the distance from the point A (x0, y0) and the angle 从单独的 X 和 Y 列在 Pandas 中创建欧几里得距离列 - Creating a Euclidian Distance Column in Pandas from Separate X and Y Columns 给定具有已知start(x,y),end(x,y)和角度的圆弧,如何计算其边界框? - Given an arc with a known start(x, y), end(x, y) and angle, how can I calculate its bounding box? Python:(x,y)平面中一堆点之间的平均距离 - Python: average distance between a bunch of points in the (x,y) plane
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM