Pandas DataFrame sort by categorical column but by specific class ordering
<p>I would like to select the top entries in a Pandas dataframe base on the entries of a specific column by using <code>df_selected = df_targets.head(N)</code>.  </p>

<p>Each entry has a <code>target</code> value (by order of importance):  </p>

<pre><code>Likely Supporter, GOTV, Persuasion, Persuasion+GOTV  
</code></pre>

<p>Unfortunately if I do </p>

<pre><code>df_targets = df_targets.sort("target")
</code></pre>

<p>the ordering will be alphabetical (<code>GOTV</code>,<code>Likely Supporter</code>, ...).</p>

<p>I was hoping for a keyword like <code>list_ordering</code> as in:  </p>

<pre><code>my_list = ["Likely Supporter", "GOTV", "Persuasion", "Persuasion+GOTV"] 
df_targets = df_targets.sort("target", list_ordering=my_list)
</code></pre>

<p>To deal with this issue I create a dictionary:  </p>

<pre><code>dict_targets = OrderedDict()
dict_targets["Likely Supporter"] = "0 Likely Supporter"
dict_targets["GOTV"] = "1 GOTV"
dict_targets["Persuasion"] = "2 Persuasion"
dict_targets["Persuasion+GOTV"] = "3 Persuasion+GOTV"
</code></pre>

<p>, but it seems like a non-pythonic approach.  </p>

<p>Suggestions would be much appreciated! </p>