How to sort/ group a Pandas data frame by class label or any specific column
<pre><code>class col2 col3 col4 col5
1     4    5    5    5
4     4    4.5  5.5  6
1     3.5  5    6    4.5
3     3    4    4    4
2     3    3.5  3.8  6.1
</code></pre>

<p>I have used hypothetical data in the example. The shape of the real DataFrame is 6680x1900. I have clustered these data into <code>50</code> labeled classes (1 to 50). How can I sort this data in ascending order of <code>class</code> labels?</p>

<p>I have tried:</p>

<pre><code>df.groupby([column_name_lst])["class"]
</code></pre>

<p>But it fails with this error:</p>

<blockquote>
  <p>TypeError: You have to supply one of 'by' and 'level'</p>
</blockquote>

<p>How to solve this problem? Expected output is:</p>

<pre><code>class col2 col3 col4 col5
1     4    5    5    5
1     3.5  5    6    4.5
2     3    3.5  3.8  6.1
3     3    4    4    4
4     4    4.5  5.5  6
</code></pre>

<p>I think you can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html" rel="nofollow"><code>DataFrame.sort_values</code></a> if <code>class</code> is <code>Series</code>:</p>

<pre><code>print (type(df['class']))
&lt;class 'pandas.core.series.Series'&gt;


print (df.sort_values(by='class'))
   class  col2  col3  col4  col5
0      1   4.0   5.0   5.0   5.0
2      1   3.5   5.0   6.0   4.5
4      2   3.0   3.5   3.8   6.1
3      3   3.0   4.0   4.0   4.0
1      4   4.0   4.5   5.5   6.0
</code></pre>

<p>Also if need <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="nofollow"><code>groupby</code></a>, use parameter <code>by</code>:</p>

<pre><code>print (df.groupby(by='class').sum())
       col2  col3  col4  col5
class                        
1       7.5  10.0  11.0   9.5
2       3.0   3.5   3.8   6.1
3       3.0   4.0   4.0   4.0
4       4.0   4.5   5.5   6.0
</code></pre>

<hr>

<p>And if <code>class</code> is <code>index</code>, use <a href="http://stackoverflow.com/a/39653884/2901002"><code>Kartik solution</code></a>:</p>

<pre><code>print (df.index)
Int64Index([1, 4, 1, 3, 2], dtype='int64', name='class')

print (df.sort_index())
       col2  col3  col4  col5
class                        
1       4.0   5.0   5.0   5.0
1       3.5   5.0   6.0   4.5
2       3.0   3.5   3.8   6.1
3       3.0   4.0   4.0   4.0
4       4.0   4.5   5.5   6.0
</code></pre>

<p>Also if need <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html" rel="nofollow"><code>groupby</code></a>, use parameter <code>level</code>:</p>

<pre><code>print (df.groupby(level='class').sum())
       col2  col3  col4  col5
class                        
1       7.5  10.0  11.0   9.5
2       3.0   3.5   3.8   6.1
3       3.0   4.0   4.0   4.0
4       4.0   4.5   5.5   6.0
</code></pre>

<p>or <code>index</code>, but first solution is better, because is more general:</p>

<pre><code>print (df.groupby(df.index).sum())
       col2  col3  col4  col5
class                        
1       7.5  10.0  11.0   9.5
2       3.0   3.5   3.8   6.1
3       3.0   4.0   4.0   4.0
4       4.0   4.5   5.5   6.0
</code></pre>

python sorting pandas dataframe group-by