title: Pandas: groupby unstack, duplicate index error
body: <p>I'm having trouble pivoting/reshaping my dataframe. After a <code>melt()</code> operation, I have the following df:</p>
  <pre><code>|----|------|------|------------|
  | ID | Rank | Var  | Val        |
  |----|------|------|------------|
  |  1 |   1  | date | 2020-01-01 |
  |----|------|------|------------|
  |  1 |   2  | date | 2020-02-01 |
  |----|------|------|------------|
  |  2 |   1  | date | 2020-01-01 |
  |----|------|------|------------|
  |  2 |   2  | date | 2020-02-01 |
  |----|------|------|------------|
  |  1 |   1  | amt  | 320        |
  |----|------|------|------------|
  |  1 |   2  | amt  | 480        |
  |----|------|------|------------|
  |  2 |   1  | amt  | 620        |
  |----|------|------|------------|
  |  2 |   2  | amt  | 400        |
  |----|------|------|------------|
  </code></pre>
  <p>The result I am looking for would be to convert the rank values into columns:</p>
  <pre><code>|----|------|------------|------------|
  | ID | Var  | 1          | 2          |
  |----|------|------------|------------|
  | 1  | date | 2020-01-01 | 2020-02-01 |
  |    |------|------------|------------|
  |    | amt  | 320        | 480        |
  |----|------|------------|------------|
  | 2  | date | 2020-01-01 | 2020-02-01 |
  |    |------|------------|------------|
  |    | amt  | 620        | 400        |
  |----|------|------------|------------|
  </code></pre>
  <p>Immediately trying to <code>unstack(level='Rank')</code> results in a</p>
  <blockquote>
  <p>ValueError: Index contains duplicate entries, cannot reshape</p>
  </blockquote>
  <p>Ok, we do have duplicate <code>IDs</code>, let's <code>groupby</code> to consolidate:</p>
  <p><code>df.set_index(['ID', 'Rank']).groupby(['ID', 'Rank']).apply(lambda x: x)</code></p>
  <p>The <code>apply()</code> is just so we can return a dataframe and preview the results, which are:</p>
  <pre><code>|----|------|------|------------|
  | ID | Rank | Var  | Val        |
  |----|------|------|------------|
  |  1 |   1  | date | 2020-01-01 |
  |    |------|------|------------|
  |    |   1  | amt  | 320        |
  |    |------|------|------------|
  |    |   2  | date | 2020-02-01 |
  |    |------|------|------------|
  |    |   2  | amt  | 480        |
  |----|------|------|------------|
  |  2 |   1  | date | 2020-01-01 |
  |    |------|------|------------|
  |    |   2  | date | 2020-02-01 |
  |    |------|------|------------|
  |    |   1  | amt  | 620        |
  |    |------|------|------------|
  |    |   2  | amt  | 400        |
  |----|------|------|------------|
  </code></pre>
  <p>This is closer. Now we just need to convert Rank into cols:</p>
  <p><code>df.set_index(['ID', 'Rank']).groupby(['ID', 'Rank']).apply(lambda x: x).unstack(level='Rank')</code></p>
  <blockquote>
  <p>ValueError: Index contains duplicate entries, cannot reshape</p>
  </blockquote>
  <p>????</p>
  <p>I also tried using <code>pivot_table</code>, but not all of the columns are numeric (the above example is simplified, but you can see date in here, which throws off pivot_table).</p>
  <p>I seem to be just one method away, but I've tried unstacking, reindexing, pivoting, <code>explode()</code>-ing, I just can't figure out this last part...</p>
  <p>How can I get the desired dataframe?</p>
  <p>Thank you!</p>