title: Getting a JSON from a stacked pandas dataframe
body: <p>I have the sample dataframe below:</p>
  <pre><code>d = {'key': ['foo', 'foo', 'foo', 'foo', 'bar', 'bar', 'bar', 'bar', 'crow', 'crow', 'crow', 'crow'], 
       'count': [12, 3, 5, 5, 3, 1, 4, 1, 7, 3, 8, 2],
       'text': [&quot;hello&quot;, &quot;i&quot;, &quot;am&quot;, &quot;a&quot;, &quot;piece&quot;, &quot;of&quot;, &quot;text&quot;, &quot;have&quot;, &quot;a&quot;, &quot;nice&quot;, &quot;day&quot;, &quot;friends&quot;],
  }
  
  }
  df = pd.DataFrame(data=d)
  df   
  </code></pre>
  <p>output:</p>
  <pre><code>    key count   text
  0   foo    12   hello
  1   foo     3   i
  2   foo     5   am
  3   foo     5   a
  4   bar     3   piece
  5   bar     1   of
  6   bar     4   text
  7   bar     1   have
  8   crow    7   a
  9   crow    3   nice
  10  crow    8   day
  11  crow    2   friends
  </code></pre>
  <p>I stacked the dataframe with:
  <code>df.set_index(&quot;key&quot;).stack()</code></p>
  <p>To get:</p>
  <pre><code>key        
  foo   count         12
        text       hello
        count          3
        text           i
        count          5
        text          am
        count          5
        text           a
  bar   count          3
        text       piece
        count          1
        text          of
        count          4
        text        text
        count          1
        text        have
  crow  count          7
        text           a
        count          3
        text        nice
        count          8
        text         day
        count          2
        text     friends
  dtype: object
  </code></pre>
  <p>I am now trying to output the stacked df as a JSON file, but when I use <code>to_json()</code>, I get the error:</p>
  <pre><code>ValueError: Series index must be unique for orient='index'
  </code></pre>
  <p>The expect output would <code>text</code> and <code>count</code> grouped by the <code>key</code>:</p>
  <pre><code>[
    {
      &quot;key&quot;: &quot;19&quot;,
      &quot;values&quot;: [
          {
              text: 'hello',
              count: 12
          },
          {
              content: 'i',
              count: 3
          },
          {
              content: 'am',
              count: 5
          },
          ...
      ]
  ]
  </code></pre>