簡體   English   中英

如何使用熊貓數據框計算點之間的距離?

[英]How do I compute distance between points using pandas dataframes?

我使用 python 3,我想使用距離公式來繪制 x、y、z 分量相對於我的名為 JCFI 的文件的差異。 我與之比較的另一個文件稱為 CALV。 我使用 Pandas 將它們作為數據幀導入,然后將它們拆分,使它們具有相同的長度並且每個文件中的日期匹配。

這是我正在嘗試的。

#matching dates between JCFI and CALV
date = JCFIdf.iloc[:1694,:1]
xcomp = JCFIdf.iloc[:1694,1:2]
ycomp = JCFIdf.iloc[:1694,2:3]
zcomp = JCFIdf.iloc[:1694,3:4] 
xcomp2 = CALVdf.iloc[201:1520,1:2]
ycomp2 = CALVdf.iloc[201:1520,2:3]
zcomp2 = CALVdf.iloc[201:1520,3:4]
diffx = (xcomp2 - xcomp)
diffy = (ycomp2 - ycomp)
diffz = (zcomp2 - zcomp)

#compute distance
distance = sqrt((diffx)**2 + (diffy)**2 + (diffz)**2) 
print(distance)

我得到的錯誤是“TypeError: must be real number, not DataFrame”

所以我想知道我是否必須以某種方式從數據幀中提取所有值才能放入距離公式中,或者熊貓中是否有函數以便我可以完成這項工作?

這是使用 numpy 的矢量化解決方案:

import pandas as pd
import numpy as np
dist = np.sqrt((df.xcomp - df.xcomp2) ** 2 +
               (df.ycomp - df.ycomp2) ** 2 +
               (df.zcomp - df.zcomp2) ** 2)

#if you want to put the result in df
df['dist'] = pd.Series(dist)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM