Sort by certain order (Situation: pandas DataFrame Groupby)
<p>I want to change the day of order presented by below code.<br>
What I want is a result with the order (Mon, Tue, Wed, Thu, Fri, Sat, Sun) <br> - should I say, sort by key in certain predefined order?</p>

<hr>

<p>Here is my code which needs some tweak:</p>

<pre><code>f8 = df_toy_indoor2.groupby(['device_id', 'day'])['dwell_time'].sum()

print(f8)
</code></pre>

<p>Current result:</p>

<pre><code>device_id                         day
device_112                        Thu     436518
                                  Wed     636451
                                  Fri     770307
                                  Tue     792066
                                  Mon     826862
                                  Sat     953503
                                  Sun    1019298
device_223                        Mon    2534895
                                  Thu    2857429
                                  Tue    3303173
                                  Fri    3548178
                                  Wed    3822616
                                  Sun    4213633
                                  Sat    4475221
</code></pre>

<p>Desired result:</p>

<pre><code>device_id                         day
device_112                        Mon     826862  
                                  Tue     792066
                                  Wed     636451 
                                  Thu     436518
                                  Fri     770307
                                  Sat     953503
                                  Sun    1019298
device_223                        Mon    2534895
                                  Tue    3303173
                                  Wed    3822616
                                  Thu    2857429
                                  Fri    3548178
                                  Sat    4475221
                                  Sun    4213633
</code></pre>

<hr>

<p>Here, <code>type(df_toy_indoor2.groupby(['device_id', 'day'])['dwell_time'])</code> is a class 'pandas.core.groupby.SeriesGroupBy'.</p>

<p>I have found <code>.sort_values()</code> , but it is a built-in sort function by values. <br>
 I want to get some pointers to set some order to use it further data manipulation.<br>
Thanks in advance.</p>

<p>Probably not the best way, but as far as I know you cannot pass a function/mapping to <code>sort_values</code>. As a workaround, I generally use <code>assign</code> to add a new column and sort by that column. In your example, that also requires resetting the index first (and setting it back).</p>

<pre><code>days = {'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sun': 6, 'Sat': 7}
f8 = f8.reset_index()
(f8.assign(day_num=f8['day'].map(days))
   .sort_values(['device_id', 'day_num'])
   .set_index(['device_id', 'day'])
   .drop('day_num', axis=1))
Out: 
                                            0
device_id                        day         
0d4fd55bb363bf6f6f7f8b3342cd0467 Mon   826862
                                 Tue   792066
                                 Wed   636451
                                 Thu   436518
                                 Fri   770307
                                 Sun  1019298
                                 Sat   953503
f6258edf9145d1c0404e6f3d7a27a29d Mon  2534895
                                 Tue  3303173
                                 Wed  3822616
                                 Thu  2857429
                                 Fri  3548178
                                 Sun  4213633
                                 Sat  4475221
</code></pre>

python sorting pandas