title: Convert Pandas DataFrame to JSON as element of larger data structure
body: <p>I've been working with pandas DataFrame objects in my server, converting them to CSV for transmission to the browser, where the tabular values are plotted using d3. While CSV is file as far as it goes, I really need more than just a 2D table of data. If nothing else, I'd like to return some metadata about the data.</p>
  
  <p>So I started messing around with JSON thinking I would be able to construct a dictionary with some meta information and my DataFrame. For example, just as an absurdly simple example:</p>
  
  <pre><code>&gt;&gt;&gt; z = numpy.zeros(10)
  &gt;&gt;&gt; df = pandas.DataFrame(z)
  &gt;&gt;&gt; df
     0
  0  0
  1  0
  2  0
  3  0
  4  0
  5  0
  6  0
  7  0
  8  0
  9  0
  &gt;&gt;&gt; result = {
  ...   "name": "Simple Example",
  ...   "data": df,
  ... }
  </code></pre>
  
  <p>Not surprisingly, that can't be directly serialized using the json module. I found the jsonext module and tried it. It "works", but produces incomplete results:</p>
  
  <pre><code>&gt;&gt;&gt; jsonext.dumps(result)
  '{"data": ["0"], "name": "Simple Example"}'
  </code></pre>
  
  <p>Looking at the methods DataFrame itself provides for this sort of thing, I found to_dict() and to_json(). The former produces dictionaries of dictionaries:</p>
  
  <pre><code>&gt;&gt;&gt; df.to_dict()
  {0: {0: 0.0, 1: 0.0, 2: 0.0, 3: 0.0, 4: 0.0, 5: 0.0, 6: 0.0, 7: 0.0, 8: 0.0, 9: 0.0}}
  </code></pre>
  
  <p>but as you can see, they can't be serialized to JSON, since the keys are not strings.</p>
  
  <p>df.to_json() looked like it might work, though I would then wind up with a JSON string embedded in aanother JSON string. Something like this:</p>
  
  <blockquote>
    <blockquote>
      <blockquote>
        <p>json.dumps({"name": "Simple Example", "data": df.to_json()})
        '{"data": "{\"0\":{\"0\":0.0,\"1\":0.0,\"2\":0.0,\"3\":0.0,\"4\":0.0,\"5\":0.0,\"6\":0.0,\"7\":0.0,\"8\":0.0,\"9\":0.0}}", "name": "Simple Example"}'</p>
      </blockquote>
    </blockquote>
  </blockquote>
  
  <p>In other words, a bit of a mess.</p>
  
  <p>Any suggestions about how to handle this sort of nested structure where some of the elements can't be directly serialized? I think I might be able to get jsonext to work, but its Dict mixin expects to find a proper (in its mind) to_dict() method. DataFrame.to_dict() doesn't seem to return the right thing. (Though I will continue to horse around with it.)</p>
  
  <p>I figured this must be a cat which has already been skinned. I just haven't found it. I'd be happy for now with nothing more hierarchical than something like my example (though with more key/value pairs), though I won't turn my nose up at a more general solution.</p>