我有my_dataframe
:
data | lat | lon |
------------+-----------------+--
10 | 1.0 | 3.0
11 | 2.0 | 4.0
21 | 9.0 | 3.0
我还有一个 accepted_coordinates = [(1.0, 3.0), (2.0, 4.0)]
列表。
我需要从数据框中删除纬度/经度不在 accepted_coordinates
中的所有行,因此最终答案应该是一个如下所示的数据框:
data | lat | lon |
------------+-----------------+--
10 | 1.0 | 3.0
11 | 2.0 | 4.0
我用 groupby
和 isin
尝试了一些事情,但不知道该怎么做。
注意:如果两个坐标都在 accepted_coordinates
中但顺序错误,则应删除该行。即,如果有一行坐标 (1.0, 4.0)
最佳答案
我会将 accepted_coordinates
转换为数据帧:
acc_coor = pd.DataFrame(accepted_coordinates, columns=['lat', 'lon'])
和 merge
与 my_dataframe
:
res = my_dataframe.merge(acc_coor, on=['lat', 'lon'], how='inner')
res
data lat lon
0 10 1.0 3.0
1 11 2.0 4.0
关于python - 如果坐标对不在列表中,则从数据框中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58760327/