【问题标题】:Sorting a Dataframe based on specified indices根据指定的索引对数据框进行排序
【发布时间】:2020-01-04 20:15:52
【问题描述】:

目前我有一个名为world 的GeoDataframe,看起来像这样,其中几何表示一个点(纬度,经度)。

      name            geometry
0   Vatican City    POINT (12.45338654497177 41.90328217996012)
1   San Marino      POINT (12.44177015780014 43.936095834768)
2   Vaduz           POINT (9.516669472907267 47.13372377429357)
3   Luxembourg      POINT (6.130002806227083 49.61166037912108)
4   Palikir         POINT (158.1499743237623 6.916643696007725)

给定另一个点(纬度,经度),我想找到从该数据帧到该特定点的最近点。我用距离函数计算最近的点

world.distance(Point(9.0,49.0)).sort_values()

现在我有一个看起来像这样的系列

2        1.936475
20       2.586576
3        2.934453
158      5.016402
172      5.284416
          ...    
6      179.671568
91     182.256232
69     188.780771
126    191.351813
122    197.120844
Length: 202, dtype: float64

如何获得以相同顺序排序的world Geodataframe 的副本,使其如下所示?谢谢!

    name            geometry
2   Vaduz           POINT (9.516669472907267 47.13372377429357)
20   ....           ......................

【问题讨论】:

    标签: python pandas geopandas


    【解决方案1】:

    您可以使用.loc 将DataFrame 重新排列到您喜欢的任何索引:

    world = world.loc[world.distance(Point(9.0,49.0)).sort_values().index]
    

    【讨论】:

      【解决方案2】:

      看来你需要reindex。使用计算距离的结果数据框重新索引world:

      world.reindex(world.distance(Point(9.0,49.0)).sort_values().index)
      

      【讨论】:

        猜你喜欢
        • 2017-01-12
        • 2021-10-03
        • 2017-10-02
        • 2013-12-15
        • 1970-01-01
        • 1970-01-01
        • 2021-07-03
        • 2019-10-24
        • 2020-06-18
        相关资源
        最近更新 更多