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

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

根據列值對之間的最近匹配合并兩個數據幀

根據列值對之間的最近匹配合并兩個數據幀

搖曳的薔薇 2023-06-20 14:28:42
我正在嘗試根據列值對之間的匹配項合并兩個數據框。但是,從一個數據幀到下一個數據幀的列值并不準確。這些對是使用瑞士坐標系的坐標,但在每個 df 中從略微不同的參考點測量。我的數據示例:df1 = pd.DataFrame({'Ecode': [2669827.294, 2669634.483, 2669766.266, 2669960.683],? ? ? ? ? ? ? ? ? ? 'Ncode': [1261034.528, 1262412.587, 1261209.646, 1262550.374],? ? ? ? ? ? ? ? ? ? 'shape': ['square', 'square', 'triangle', 'circle']})df1? ? ?Ecode? ? ? ? ? ? Ncode? ? ? ? ? shape0? ?2669827.294? ? ?1261034.528? ? ?square1? ?2669634.483? ? ?1262412.587? ? ?square2? ?2669766.266? ? ?1261209.646? ? ?triangle3? ?2669960.683? ? ?1262550.374? ? ?circledf2 = pd.DataFrame({'CoorE': [2669636, 2669765, 2669827, 2669961],? ? ? ? ? ? ? ? ? ? 'CoorN': [1262413, 1261211, 1261032, 1262550],? ? ? ? ? ? ? ? ? ? 'color': ['purple', 'blue', 'blue', 'yellow']})df2? ? ?CoorE? ? ? ?CoorN? ? ? color0? ?2669636? ? ?1262413? ? ?purple1? ?2669765? ? ?1261211? ? ?blue2? ?2669827? ? ?1261032? ? ?blue3? ?2669961? ? ?1262550? ? ?yellow我有我想比較的數據,位于兩組坐標(例如“形狀”和“顏色”)。我想要的結果與最接近匹配的列對匹配:? ? ?CoorE? ? ? ?CoorN? ? ? color? ?shape0? ?2669636? ? ?1262413? ? ?purple? square1? ?2669765? ? ?1261211? ? ?blue? ? triangle2? ?2669827? ? ?1261032? ? ?blue? ? square3? ?2669961? ? ?1262550? ? ?yellow? circle有沒有辦法做到這一點?我曾嘗試使用 merge_asof 但意識到它不能鍵控兩個變量。我還看到線程根據緯度和經度計算這個。我可以編寫一個函數,將 CoorE/CoorN 和 Ecode/Ncode 視為 x/y 坐標,并計算一對坐標之間的距離(可能有更好的方法,但我是新手):import math??def calculateDistance(x1,y1,x2,y2):??? ? ?dist = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)??? ? ?return dist??print calculateDistance(x1, y1, x2, y2)或類似的東西,但無法弄清楚如何使用這種函數根據最小距離比較和匹配來自兩個獨立數據幀的坐標對。真實的數據集也有大約 300 萬個條目,我想知道執行此操作的內存占用最少的方法是什么。
查看完整描述

1 回答

?
慕勒3428872

TA貢獻1848條經驗 獲得超6個贊

要使用庫來計算距離,您需要在統一系統上。來自谷歌,我相信你正在使用epsg:21781

  1. 首先使用標準化坐標系pyproj

  2. 做顏色形狀的笛卡爾積

  3. 使用計算這些之間的距離geopy

  4. 您現在可以選擇您想要的結果行。舉個例子,我在按顏色形狀分組時采取了最近的做法

import pyproj, geopy.distance

df1 = pd.DataFrame({'Ecode': [2669827.294, 2669634.483, 2669766.266, 2669960.683],

                    'Ncode': [1261034.528, 1262412.587, 1261209.646, 1262550.374],

                    'shape': ['square', 'square', 'triangle', 'circle']})

df2 = pd.DataFrame({'CoorE': [2669636, 2669765, 2669827, 2669961],

                    'CoorN': [1262413, 1261211, 1261032, 1262550],

                    'color': ['purple', 'blue', 'blue', 'yellow']})



# assuming this co-ord system https://epsg.io/21781 then mapping to https://epsg.io/4326

sc = pyproj.Proj("epsg:21781")

dc = pyproj.Proj("epsg:4326")


df1 = df1.assign(

    shape_gps=lambda x: x.apply(lambda r: pyproj.transform(sc, dc, r["Ecode"], r["Ncode"]), axis=1)

)

df2 = df2.assign(

    color_gps=lambda x: x.apply(lambda r: pyproj.transform(sc, dc, r["CoorE"], r["CoorN"]), axis=1)

)


(df1

     .assign(foo=1)

     .merge(df2.assign(foo=1), on="foo")

     .assign(distance=lambda x: x.apply(lambda r: 

                                        geopy.distance.geodesic(r["color_gps"], r["shape_gps"]).km, axis=1))

     .sort_values("distance")

 .groupby(["color","shape"]).agg({"distance":"first","CoorE":"first","CoorN":"first"})

)

為最近的合并更新

如果你選擇一個參考點來計算距離,你會得到你想要的。


import pyproj, geopy.distance

df1 = pd.DataFrame({'Ecode': [2669827.294, 2669634.483, 2669766.266, 2669960.683],

                    'Ncode': [1261034.528, 1262412.587, 1261209.646, 1262550.374],

                    'shape': ['square', 'square', 'triangle', 'circle']})

df2 = pd.DataFrame({'CoorE': [2669636, 2669765, 2669827, 2669961],

                    'CoorN': [1262413, 1261211, 1261032, 1262550],

                    'color': ['purple', 'blue', 'blue', 'yellow']})



# assuming this co-ord system https://epsg.io/21781 then mapping to https://epsg.io/4326

sc = pyproj.Proj("epsg:21781")

dc = pyproj.Proj("epsg:4326")

# pick a reference point for use in diatnace calcs

refpoint = pyproj.transform(sc, dc, df1.loc[0,["Ecode"]][0], df1.loc[0,["Ncode"]][0])


df1 = df1.assign(

    shape_gps=lambda x: x.apply(lambda r: pyproj.transform(sc, dc, r["Ecode"], r["Ncode"]), axis=1),

    distance=lambda x: x.apply(lambda r: geopy.distance.geodesic(refpoint, r["shape_gps"]).km, axis=1),

).sort_values("distance")

df2 = df2.assign(

    color_gps=lambda x: x.apply(lambda r: pyproj.transform(sc, dc, r["CoorE"], r["CoorN"]), axis=1),

    distance=lambda x: x.apply(lambda r: geopy.distance.geodesic(refpoint, r["color_gps"]).km, axis=1),

).sort_values("distance")


# no cleanup of columns but this works

pd.merge_asof(df1, df2, on="distance", direction="nearest")


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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