title: Pandas value_counts with manual labels and sorting
body: <p>I have a field containing codes (DMDEDUC2 in this example). I'd like to calculate a frequency table (value_counts) on this field and display it with user-specified labels. The code below achieves exactly what I want... but it feels to me like I have surely missed a more <strong>standard</strong> way to achieve the intended result.</p>
  <p>Logically, the <code>value_counts</code> and the <code>replace</code> lines cannot be simplified. But surely the rest could be more elegant.</p>
  <p>Is there a simpler way to get to this result? A more panda-ish solution?</p>
  <pre><code># Tiny dataset for clarity
  import pandas as pd
  df = pd.DataFrame({ 'DMDEDUC2': [5, 3, 3, 5, 4, 2, 4, 4] })
  d = {
        1: &quot;&lt;9&quot;
      , 2: &quot;9-11&quot;
      , 3: &quot;HS/GED&quot;
      , 4: &quot;Some college/AA&quot;
      , 5: &quot;College&quot;
      , 7: &quot;Refused&quot;
      , 9: &quot;Don't know&quot;
  }
  
  # First get value counts (vc) for DMDEDUC2
  # This line gets all the data I need in the correct order... 
  # but without the labels I need.
  vc = df.DMDEDUC2.value_counts().sort_index()
  
  # Convert the resulting Series to a DataFrame 
  # to allow for clear labels in a logical order
  vc = vc.to_frame()
  vc['DMDEDUC2x'] = vc.index
  vc.DMDEDUC2x = vc.DMDEDUC2x.replace(d)
  vc = vc.set_index('DMDEDUC2x')
  vc = vc.rename({'DMDEDUC2':'COUNTS'}, axis=1)
  print(vc)
  </code></pre>
  <p>Desired output (it's ordered by the [non-displayed] code, not by value or label):</p>
  <pre><code>                 COUNTS
  DMDEDUC2x              
  &lt;9                  655
  9-11                643
  HS/GED             1186
  Some college/AA    1621
  College            1366
  Don't know            3
  </code></pre>
  <p>Desired output for the tiny sample dataset:</p>
  <pre><code>                 COUNTS
  DMDEDUC2x              
  9-11                  1
  HS/GED                2
  Some college/AA       3
  College               2
  </code></pre>