如何根据另一个数据框的最小值和最大值创建列
How to create a column based on the min and max of another dataframe
我有两个数据框,一个包含一组地点的纬度和经度的最小值和最大值,第二个包含特定地点的纬度和经度。
df1
group_places
min_lat
max_lat
min_long
max_long
A
39.1
40.1
9.2
10.5
B
38.10
39.05
8.1
9
C
33.1
35.1
7.1
8
D
31.1
35
6.1
7
df2
place
lat
long
f
34.1
7.9
g
39.2
9.8
h
31.2
6.8
我需要在 df2 中创建一个列,根据 df1 中的纬度和经度范围显示 group_places。 (纬度和经度只是一个例子)
df_result
place
lat
long
group
f
34.1
7.9
C
g
39.2
9.8
A
h
31.2
6.8
D
提前致谢。
以你的数据量,cross-merge数据大概200M行,应该可以:
(df2.merge(df1, on='key')
.query('(min_lat<=lat<=max_lat) and (min_long<=long<=max_long)')
[['place','lat','long', 'group_places']]
)
输出:
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
我有两个数据框,一个包含一组地点的纬度和经度的最小值和最大值,第二个包含特定地点的纬度和经度。
df1
group_places | min_lat | max_lat | min_long | max_long |
---|---|---|---|---|
A | 39.1 | 40.1 | 9.2 | 10.5 |
B | 38.10 | 39.05 | 8.1 | 9 |
C | 33.1 | 35.1 | 7.1 | 8 |
D | 31.1 | 35 | 6.1 | 7 |
df2
place | lat | long |
---|---|---|
f | 34.1 | 7.9 |
g | 39.2 | 9.8 |
h | 31.2 | 6.8 |
我需要在 df2 中创建一个列,根据 df1 中的纬度和经度范围显示 group_places。 (纬度和经度只是一个例子)
df_result
place | lat | long | group |
---|---|---|---|
f | 34.1 | 7.9 | C |
g | 39.2 | 9.8 | A |
h | 31.2 | 6.8 | D |
提前致谢。
以你的数据量,cross-merge数据大概200M行,应该可以:
(df2.merge(df1, on='key')
.query('(min_lat<=lat<=max_lat) and (min_long<=long<=max_long)')
[['place','lat','long', 'group_places']]
)
输出:
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