亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將最近的線關聯到 Python 中的每個給定點?

如何將最近的線關聯到 Python 中的每個給定點?

千巷貓影 2023-04-11 15:48:42
我有兩個 Pandas DataFrame,第一個名為Points,列為“ longitude ”和“ latitude ”(即地理坐標);并且,第二個數據框被命名為具有以下列的鏈接:第一個點為“ lon1 ”和“ lat1 ”,第二個點為“ lon2 ”和“ lat2 ”然后每行中給出的每對點創建一個鏈接/線. 此外,對于Links DataFrame,它有一個名為“ link_id ”的列。比如說,大約有 10 個點和 4,000 個鏈接。我如何通過返回'link_id'并將其作為名為' closest_link '的附加列附加到Points DataFrame來將每個給定點關聯到最近的鏈接/線?
查看完整描述

1 回答

?
森欄

TA貢獻1810條經驗 獲得超5個贊

這是一種可能有效的方法??紤]:

  1. 在兩個數據框PointsLinks之間生成叉積,

  2. 然后對新 DataFrame 中的每一應用一個函數。

  3. 查找函數為每個組報告的最小距離。

我們將新的 df 稱為PointsLinks。

下面是一些采用這種方法的代碼:

import pandas as pd

import random        


Points = pd.DataFrame( [ [ 1,2 ], [ 3,4 ], [ 5,6 ] ], columns = [ 'longitude', 'latitude' ] )

Links = pd.DataFrame( [ [ 'Link1', ( 4,3 ) , ( -1, -2 ) ], [ 'Link2', (10,10) , ( -5, -5 ) ] ], columns = [ 'linkid', 'lon1&lat1', 'lon2&lat2' ] )


   

print(Points) 

print(Links)         


#Step 1:  https://stackoverflow.com/questions/53699012/performant-cartesian-product-cross-join-with-pandas

def cartesian_product_basic(left, right):

    return (         

       left.assign(key=1).merge(right.assign(key=1), on='key').drop('key', 1))

   

def DistanceToLink( pointlink ): 

  return random.randrange(10)  


PointsLinks = cartesian_product_basic(Points,Links)       

print( PointsLinks ) 


#Step 2: https://stackoverflow.com/questions/26886653/pandas-create-new-column-based-on-values-from-other-columns-apply-a-function-o

PointsLinks['distance'] = PointsLinks.apply( lambda row : DistanceToLink(row), axis = 'columns' )



print( PointsLinks )


#Step 3:  Find the smallest distance per group https://stackoverflow.com/questions/27842613/pandas-groupby-sort-within-groups

closest = PointsLinks.sort_values( [ 'latitude', 'longitude', 'distance' ] , ascending = True ).groupby(  [ 'latitude', 'longitude'] ).head(1)


# Drop the unnecessary columns

closest.drop( columns = ['lon1&lat1','lon2&lat2','distance'] , inplace=True) 

print(closest)

以下是代碼創建的數據框:


要點:


   longitude  latitude

0          1         2

1          3         4

2          5         6 

鏈接:


  linkid lon1&lat1 lon2&lat2

0  Link1    (4, 3)  (-1, -2)

1  Link2  (10, 10)  (-5, -5)

然后是 PointsLinks(在使用 apply() 添加距離列之后:


   longitude  latitude linkid lon1&lat1 lon2&lat2  distance

0          1         2  Link1    (4, 3)  (-1, -2)         1

1          1         2  Link2  (10, 10)  (-5, -5)         6

2          3         4  Link1    (4, 3)  (-1, -2)         0

3          3         4  Link2  (10, 10)  (-5, -5)         9

4          5         6  Link1    (4, 3)  (-1, -2)         5

5          5         6  Link2  (10, 10)  (-5, -5)         1

我沒有實施DistanceToLink。我只是在那里放了一個隨機數生成器。這是第一個pointlink對象的樣子(它是一個代表一行的系列):


longitude           1

latitude            2

linkid          Link1

lon1&lat1      (4, 3)

lon2&lat2    (-1, -2)

現在您有了每個組合的距離,您可以找到并選擇具有最短距離的 PointLink 對(使用pandas groupby sort within groups):


closest = PointsLinks.sort_values( [ 'latitude', 'longitude', 'distance' ] , ascending = True ).groupby(  [ 'latitude', 'longitude'] ).head(1)

以下是結果:


   longitude  latitude linkid

0          1         2  Link1

2          3         4  Link1

5          5         6  Link2


查看完整回答
反對 回復 2023-04-11
  • 1 回答
  • 0 關注
  • 119 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號