用熊猫计算具有UTM坐标的数据框的距离

时间:2019-06-27 08:14:28

标签: python pandas distance utm

我有一个庞大的dataframe。结构数据如下:

df
ID  Annotation  X           Y
A   Boarding    767513.9918 9425956.2571
A   Alighting   767154.1396 9427584.0004
B   Boarding    767450.5277 9432627.9543
B   Alighting   767495.0101 9426797.1772
C   Boarding    767648.9507 9426442.5497
C   Alighting   767037.0309 9428878.9032
........

X和Y数据使用UTM坐标。我想计算每个ID登机与下车之间的距离。我的问题很安静,但与此问题(Distance matrix in Python Pandas)不同。我的预期结果如下:

result
ID  Anotation_1 X_1         Y_1         Anotation_2 X_2         Y_2      Dist
A   Boarding    767513.99   9425956.26  Alighting   767154.14   9427584.00  1667.05
B   Boarding    767450.53   9432627.95  Alighting   767495.01   9426797.18  5830.95
C   Boarding    767648.95   9426442.55  Alighting   767037.03   9428878.90  2512.02
    .......

谢谢您的帮助。

3 个答案:

答案 0 :(得分:2)

我将旋转数据框:

result = df.pivot('ID', 'Annotation', ['X', 'Y'])

获得

                      X                          Y              
Annotation    Alighting     Boarding     Alighting      Boarding
ID                                                              
A           767154.1396  767513.9918  9.427584e+06  9.425956e+06
B           767495.0101  767450.5277  9.426797e+06  9.432628e+06
C           767037.0309  767648.9507  9.428879e+06  9.426443e+06

然后,我将重命名列并重新索引:

ix = result.columns.to_frame()
result.columns = ix['Annotation'] + '_' + ix.iloc[:,0]
result = result.reindex(columns=['Alighting_X', 'Alighting_Y', 'Boarding_X', 'Boarding_Y'])

获得:

    Alighting_X   Alighting_Y   Boarding_X    Boarding_Y
ID                                                      
A   767154.1396  9.427584e+06  767513.9918  9.425956e+06
B   767495.0101  9.426797e+06  767450.5277  9.432628e+06
C   767037.0309  9.428879e+06  767648.9507  9.426443e+06

现在很容易计算距离:

result['Dist'] = np.sqrt((result.Alighting_X - result.Boarding_X)**2 + (result.Alighting_Y - result.Boarding_Y)**2)

最终得到:

    Alighting_X   Boarding_X   Alighting_Y    Boarding_Y         Dist
ID                                                                   
A   767154.1396  767513.9918  9.427584e+06  9.425956e+06  1667.045847
B   767495.0101  767450.5277  9.426797e+06  9.432628e+06  5830.946773
C   767037.0309  767648.9507  9.428879e+06  9.426443e+06  2512.023929

答案 1 :(得分:2)

我正在使用unstack()

m=(df.assign(k=(df.groupby('ID').cumcount()+1).astype(str)).
        set_index(['ID','k']).unstack().sort_values(by='k',axis=1))
m.columns=m.columns.map('_'.join)

m=m.assign(Dist=np.sqrt((m.X_1 - m.X_2)**2 + (m.Y_1 - m.Y_2)**2))
print(m)

enter image description here

答案 2 :(得分:1)

一种解决方法,假设输入是正确且正确的,将使用groupby

df = df.groupby('ID').apply(lambda x: pd.Series(x.values[0:2,2:4].flatten()))  # (*)
df.columns=['X_1','Y_1','X_2','Y_2']
#df.reset_index()  # Uncomment if you want 'ID' as a column and not an Index

至于您希望得到的结果中的其他列:Anotation_1Anotation_2始终是恒定的,因此我不必费心将它们包括在内。 Dist列-好吧,有了新的列,您现在就可以计算出它,或者您可以更改上面的代码来计算距离,同时遍历上面的步骤(*)中的数字,从而更改代码类似:(此处使用了虚拟距离计算,请用您的虚拟距离代替!)

def my_func(pdf):
    return pd.Series([pdf.values[0,2], pdf.values[0,3], pdf.values[1,2], pdf.values[1,3],
                      np.sqrt((pdf.values[0,2]-pdf.values[1,2])**2+(pdf.values[0,3]-pdf.values[1,3])**2)  # <= your distance calculation goes here...
                     ])
df = df.groupby('ID').apply(my_func)
df.columns=['X_1','Y_1','X_2','Y_2','Dist']
#df.reset_index()  # Uncomment if you want 'ID' as a column and not an Index

更新:如果您坚持要包含这些常量列,则可以稍后像这样简单地添加它们:(但是为什么会这样呢?尤其是当DataFrame ... 很大时)

df['Annotation_1'] = 'Boarding'
df['Annotation_2'] = 'Alighting'
# And if you further insist on a specific ordering of the columns, you can go with:
df = df[['Annotation_1', 'X_1', 'Y_1', 'Annotation_2', 'X_2', 'Y_2', 'Dist']]