I have a geopandas dataframe created from a shapefile.
I would like to sort my dataframe according to the column: "name" AND the line chunks should also be sorted by geographic location, such that all nearby chunks which have the same name are grouped together.
How can I do this kind of sorting ?
What I have tried: 1. I calculate the mean coordinate fro each linestring:
df['mean_coord'] = df.geometry.apply(lambda g: [np.mean(g.xy[0]),np.mean(g.xy[1])])
I group the dataframe according to the "name" column and I sort the resulting dataframe according to the mean coordinate:
grouped=df.sort_values(['mean_coord'],ascending=False).groupby('name')
But I am not sure, if this is the best/most elegant or even correct way to do it. Other than that, I don't know how to get back to a pandas dataframe from the grouped element ?