[英]How to create a column based on the min and max of another dataframe
我有兩個數據框,一個包含一組地點的緯度和經度的最小值和最大值,第二個包含特定地點的緯度和經度。
df1
group_places | min_lat | 最大緯度 | min_long | 最大長度 |
---|---|---|---|---|
一個 | 39.1 | 40.1 | 9.2 | 10.5 |
乙 | 38.10 | 39.05 | 8.1 | 9 |
C | 33.1 | 35.1 | 7.1 | 8 |
D | 31.1 | 35 | 6.1 | 7 |
df2
地方 | 緯度 | 長 |
---|---|---|
F | 34.1 | 7.9 |
G | 39.2 | 9.8 |
H | 31.2 | 6.8 |
我需要在 df2 中創建一個列,該列根據 df1 中的緯度和經度范圍顯示 group_places。 (緯度和經度只是一個例子)
df_result
地方 | 緯度 | 長 | 團體 |
---|---|---|---|
F | 34.1 | 7.9 | C |
G | 39.2 | 9.8 | 一個 |
H | 31.2 | 6.8 | D |
先感謝您。
以你的數據大小,交叉合並的數據大約是 200M 行,所以它應該是可行的:
(df2.merge(df1, on='key')
.query('(min_lat<=lat<=max_lat) and (min_long<=long<=max_long)')
[['place','lat','long', 'group_places']]
)
Output:
place lat long group_places
2 f 34.1 7.9 C
4 g 39.2 9.8 A
11 h 31.2 6.8 D
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.