Selecting row from GeoDataFrame and making it a new GeoDataFrame

2,951 views
Skip to first unread message

Dmitriy Tarasov

unread,
Sep 1, 2021, 9:54:17 AM9/1/21
to geopandas

I need to select a row (not a column) from a GeoDataFrame (called current_incident_paths) where a certain column ('length_f') has the lowest value and to turn it into a new, one-row GeoDataFrame of its own. I think I know how to select the minimum value:
shortest_path = current_incident_paths['length_f'].min()
What is the syntax for turning that row into a new GeoDataFrame, though? The code that I have so far:
shortest_path_for_incidentcurrent_incident_paths[current_incident_paths['length_f'].min()]
isn't working.

Martin Fleischmann

unread,
Sep 1, 2021, 12:26:07 PM9/1/21
to geop...@googlegroups.com
If you are interested in the lowest value, check the following snippet. `lowest` will be a single-row GeoDataFrame (assuming only one value is the lowest).

import geopandas

df = geopandas.read_file(geopandas.datasets.get_path("naturalearth_lowres"))
lowest = df[df.pop_est == df.pop_est.min()]

Another option would be to pass index or integer index within a list. Notice that there is a difference between iloc[1] and iloc[[1]]. The former returns row as a series, the latter single-row DataFrame.

df.iloc[[1]]

Martin

--
You received this message because you are subscribed to the Google Groups "geopandas" group.
To unsubscribe from this group and stop receiving emails from it, send an email to geopandas+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/geopandas/a5619aac-3065-41c9-9032-64da797604f3n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages