Preserving order of occurrence with size() function
<p>I would like to preserve the order of my DataFrame when using the .size() function. My first DataFrame is created by choosing a subset of a larger one:</p>

<pre><code>df_South = df[df['REGION_NAME'] == 'South']
</code></pre>

<p>Here is an example of what the DataFrame looks like:</p>

<p><a href="http://i.stack.imgur.com/PwW8q.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/PwW8q.jpg" alt="enter image description here"></a></p>

<p>With this DataFrame I count the occurrences of each unique 'TEMPBIN_CONS' variable.</p>

<pre><code>South_Count = df_South.groupby('TEMPBIN_CONS').size()
</code></pre>

<p>I would like to maintain the order that exists using the SORT column. I created this column based on the order I would like my 'TEMPBIN_CONS' variable to appear after counting. I can't seem to get it to appear in the proper order though. I've tried using .sort_index() on South_Count and it does not change order that groupby() creates. </p>

<p>Ultimately this is my solution for fixing the axis ordering of a bar plot I am creating of South_Count. As it is the ordering is very difficult to read and would like it to appear in a logical order. </p>

<p>For reference South_Count, and subsequently the axis of my bar plot appears in 
this order:</p>

<p><a href="http://i.stack.imgur.com/IWBqv.jpg" rel="nofollow"><img src="http://i.stack.imgur.com/IWBqv.jpg" alt="enter image description here"></a></p>

<p>Try this: </p>

<pre><code>South_Count = df_South.groupby('TEMPBIN_CONS', sort=False ).size()
</code></pre>

<p>Looks as though your data is sorted as string. </p>

python pandas numpy plot dataframe