title: How to pivot a data frame created from group using pivot
body: <ol>
  <li>updated from <strong>vendorname</strong> to <strong>companyname</strong></li>
  </ol>
  <p>2.1 after apply option 1 from @mozway, got a new error saying <em><strong>ValueError: Index contains duplicate entries, cannot reshape</strong></em>.</p>
  <p>2.1.1 update the data frame to reflect the error.(which is changing the score to good for both B and C under the same zip code that produce two ranks 1)</p>
  <p>2.1.2 using &quot;first&quot; instead of &quot;dense&quot; solved the error</p>
  <p>---updated above</p>
  <p>why my index &quot;zip&quot; couldn't be found after Groupby?</p>
  <p>I checked the type of my dataframe op (<strong>pandas.core.frame.DataFrame</strong>) which is as same as the example pandas documentation provided.<a href="http://shorturl.at/couK2" rel="nofollow noreferrer">Pandas example</a></p>
  <pre><code>df = pd.DataFrame({'zip': [&quot;46062&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;], 
                     'goodbadscore': [&quot;good&quot;,&quot;good&quot;,&quot;good&quot;,&quot;good&quot;,&quot;good&quot;,&quot;bad&quot;,&quot;bad&quot;,&quot;good&quot;,&quot;good&quot;,&quot;good&quot;,&quot;bad&quot;,&quot;good&quot;,&quot;good&quot;,&quot;good&quot;,&quot;good&quot;,&quot;good&quot;,&quot;bad&quot;,&quot;good&quot;],
                     'postlcode' : [&quot;46062&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;46062&quot;,&quot;11236&quot;,&quot;46062&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;,&quot;11236&quot;],
                     'companyname': [&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;C&quot;,&quot;C&quot;,&quot;A&quot;,&quot;A&quot;,&quot;A&quot;,&quot;B&quot;,&quot;B&quot;,&quot;A&quot;,&quot;A&quot;,&quot;B&quot;,&quot;A&quot;,&quot;A&quot;,&quot;B&quot;,&quot;A&quot;,&quot;A&quot;]}
                     )
                     
  print(df)
  print(type(df))
  
  ### count vendor under zip and count good
  op = df.groupby(['zip','companyname'], as_index=True).aggregate(
      countinzipcode=('companyname', 'count'),
      good=('goodbadscore', lambda s: s.eq('good').sum())
  )
  
  ### calculate % on time = on time count / total count
  op['ontime%'] = op['good'] / op['countinzipcode'] * 100
  
  ### total zip rank and rank within zip
  op['rankingbyallzip'] = op['ontime%'].rank(ascending=False, method='dense').astype(int)
  op['rankwithinzip'] = op.groupby(&quot;zip&quot;)[&quot;ontime%&quot;].rank(&quot;dense&quot;, ascending=False)
  print(op)
  print(type(op))
  
  ### keyerror
  op.pivot(index = &quot;zip&quot;, columns=&quot;rankwithinzip&quot;, values=&quot;company&quot;)
  </code></pre>