Currently I have a GeoDataframe called world that looks something like this, where geometry represents a Point(latitude, longitude).
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)
Given another Point(lat,long), I wanted to find the closest points from this dataframe to that particular point. I used the distance function to calculate the nearest points
world.distance(Point(9.0,49.0)).sort_values()
and now I have a series that looks like this
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
How can I get a copy of the world Geodataframe sorted in that same order so it looks like below? Thanks!
name geometry
2 Vaduz POINT (9.516669472907267 47.13372377429357)
20 .... ......................
You can do that using .loc to rearrange the DataFrame to which ever index you like:
world = world.loc[world.distance(Point(9.0,49.0)).sort_values().index]
Seems like you need reindex. Use the resulting dataframe from calculating the distance to reindex world:
world.reindex(world.distance(Point(9.0,49.0)).sort_values().index)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With