title: Sort GeoPandas by Name Of Line And By Geographic Location
body: <p>I have a geopandas dataframe created from a shapefile. </p>
  
  <p>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.</p>
  
  <p><strong>How can I do this kind of sorting ?</strong></p>
  
  <p>What I have tried:
  1. I calculate the mean coordinate fro each linestring:</p>
  
  <pre><code>df['mean_coord'] = df.geometry.apply(lambda g: [np.mean(g.xy[0]),np.mean(g.xy[1])])
  </code></pre>
  
  <ol start="2">
  <li><p>I group the dataframe according to the "name" column and I sort the resulting dataframe according to the mean coordinate:</p>
  
  <p>grouped=df.sort_values(['mean_coord'],ascending=False).groupby('name')</p></li>
  </ol>
  
  <p>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 ?</p>