Sort data within group - Pandas Dataframe
<p>I have the following data frame: </p>

<pre><code>      As  Comb     Mu(+)    Name      Zone     fÂ´c
33  0.37    2   6.408225   Beam_13   Final    30.0
29  0.37    2   6.408225   Beam_13   Begin    30.0
31  0.94    2  16.408225   Beam_13   Middle   30.0
15  0.54    2   9.504839   Beam_7    Final    30.0
11  0.54    2   9.504839   Beam_7    Begin    30.0
13  1.12    2  19.504839   Beam_7    Middle   30.0
</code></pre>

<p>I need to sort the data by <code>Name</code> and then by <code>Zone</code> within a group as shown in the expected output below:</p>

<pre><code>      As  Comb     Mu(+)    Name      Zone     fÂ´c
11  0.54    2   9.504839   Beam_7    Begin    30.0
13  1.12    2  19.504839   Beam_7    Middle   30.0
15  0.54    2   9.504839   Beam_7    Final    30.0
29  0.37    2   6.408225   Beam_13   Begin    30.0
31  0.94    2  16.408225   Beam_13   Middle   30.0
33  0.37    2   6.408225   Beam_13   Final    30.0
</code></pre>

<p>I can order by index, but not by name and zone within the <code>Name</code> group. Any ideas?</p>

<p>The cleanest way is to convert the <code>Name</code> and <code>Zone</code> columns to the category type, specifying the categories and order. </p>

<pre><code>from io import StringIO

data = """
As,Comb,Mu(+),Name,Zone,fÂ´c
33,0.37,2,6.408225,Beam_13,Final,30.0
29,0.37,2,6.408225,Beam_13,Begin,30.0
31,0.94,2,16.408225,Beam_13,Middle,30.0
15,0.54,2,9.504839,Beam_7,Final,30.0
11,0.54,2,9.504839,Beam_7,Begin,30.0
13,1.12,2,19.504839,Beam_7,Middle,30.0
"""

df = pd.read_csv(StringIO(data))

# convert Name and Zone to ordinal/category type
df.Name = df.Name.astype('category', categories=["Beam_7", "Beam_13"], ordered=True)
df.Zone = df.Zone.astype('category', categories=["Begin", "Middle", "Final"], ordered=True)

df.sort_values(by=['Name', 'Zone'])
</code></pre>

<p>Here's the output:</p>

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

<p>Other options can be found <a href="http://stackoverflow.com/a/13839029/3453737">here</a></p>

python sorting pandas group