title: After grouping and sorting: Why is my python dataframe column name row, not in a single row?
body: <p>Firstly, I am not sure that I am doing this the best way, so alternatives appreciated!</p>
  
  <p>I have a python pandas dataframe which I have created:
  <code>raw_data = {'Name' : pd.Series(['david','andrew','calvin','david','calvin','david'], index=['a', 'b', 'c', 'd','e','f']),'Age' : pd.Series([35, 40, 5, 35, 5, 35], index=['a', 'b', 'c', 'd', 'e','f'])}
  df = pd.DataFrame(raw_data, columns = ['Name','Age'])</code></p>
  
  <p>I want to now groupby and also sort (based on the frequency of the count of the occurrences of 'Name') and return the top 2 records. ie the following output:</p>
  
  <pre><code>  Name Count
  david      3
  calvin     2
  </code></pre>
  
  <p>I start by adding a "Count" column:</p>
  
  <pre><code>df = pd.DataFrame(raw_data, columns = ['Name','Age'])
  </code></pre>
  
  <p>Gives:</p>
  
  <pre><code>     Name   Age Count
  a   david   35  3
  b   andrew  39  1
  c   calvin  5   2
  d   david   34  3
  e   calvin  5   2
  f   david   35  3
  </code></pre>
  
  <p>I then can group and sort this with the below code:</p>
  
  <pre><code>df[['Name','Count']].groupby('Name').count().sort_values('Count',ascending=0).head(2)
  </code></pre>
  
  <p>This is where I have the problem, the data is "correct", but the column headers appear to be in separate rows...See the screenshot:
  <a href="http://i.stack.imgur.com/C47GM.png" rel="nofollow">Column Header Alignment problem pic</a></p>
  
  <p>I have 3 questions</p>
  
  <p>1) Why does this happen?</p>
  
  <p>2) How can I fix it?</p>
  
  <p>3) Is there a better way to do this? </p>
  
  <p>Thanks! </p>
  
  <p>[EDIT] - I have answered question 2, (How to fix it). Apparently i need to reset the index:</p>
  
  <pre><code>df.reset_index()
  </code></pre>
  
  <p>Would love to know, why and is there a better way? </p>