[英]How to group and calculate distance?
我有一個數據集如下:
ID | X | Y | Z
--------------
1 | 5 | 5 | 5
1 | 4 | 2 | 0
2 | 1 | 3 | 4
.
.
.
我有每個 ID 的真實值 (x,y,z)。 我想使用上表中每個 ID 的真實值來計算距離。 我嘗試使用df.groupby()
但不確定如何將 df 重新組合在一起。
真實值:
ID | X | Y | Z
---------------
1 | 1 | 2 | 3
2 | 4 | 5 | 6
3 | 7 | 8 | 9
.
.
我希望 output 看起來像:
ID | X | Y | Z
-----------------
1 | 4 | 3 | 2
1 | 3 | 0 | -3
2 | -3 | -2 | -2
.
.
.
您可以將ID
設置為索引並減去。 通過這樣做, pandas
將為您對齊正確的ID
(在本例中為索引):
df.set_index('ID').sub(ground_truths.set_index('ID')).reset_index()
Output:
ID X Y Z
0 1 4.0 3.0 2.0
1 1 3.0 0.0 -3.0
2 2 -3.0 -2.0 -2.0
3 3 NaN NaN NaN
更新:對於歐幾里得:
tmp = df.set_index('ID').sub(ground_truths.set_index('ID'))
# this is Euclidean part:
# you can use other packages, e.g. np.norm
result = ((tmp**2).sum(axis=1))**0.5
result = result.reset_index()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.