title: Convert a Pandas Dataframe into a nested json
body: <p>I need to convert pandas Dataframe into  nested json. The output of .to_json gives the following</p>
  
  <pre><code>{"Annual Expenditure":{"0":250,"1":250},"Annual Frequency":{"0":1,"1":1},"Avg days":{"0":null,"1":null},"First visit":{"0":1449100800000,"1":1490054400000},"Frequency":{"0":1,"1":1},"Guest":{"0":25642,"1":55521},"Last visit":{"0":1449100800000,"1":1490054400000},"Monetory":{"0":250,"1":250},"Recency":{"0":701,"1":227},"Visit_Ids":{"0":[80611],"1":[342104]},"RFMClass":{"0":"444","1":"144"},"AvgLTV":{"0":13305.7692307692,"1":13305.7692307692}}
  </code></pre>
  
  <p>But I need in a nested json with key as the guest_id and there corresponding values. Something like this:</p>
  
  <pre><code>{55521: {'Monetory': 250, 'First visit': datetime.date(2017, 3, 21), 'Annual Expenditure': 250, 'Frequency': 1, 'Last visit': datetime.date(2017, 3, 21), 'Avg days': NaT, 'Annual Frequency': 1, 'Recency': 227, 'Visit_Ids': [342104]}, 25642: {'Monetory': 250, 'First visit': datetime.date(2015, 12, 3), 'Annual Expenditure': 250, 'Frequency': 1, 'Last visit': datetime.date(2015, 12, 3), 'Avg days': NaT, 'Annual Frequency': 1, 'Recency': 701, 'Visit_Ids': [80611]}}
  </code></pre>
  
  <p>The .to_json() function doesn't work the way I want it and I have pretty much exhausted all my options on this. Any Idea how to proceed ?</p>
  
  <p>EDIT:</p>
  
  <p>Thanks for the answer! I'm getting json outputs as part of a loop, and each output I get is a unique JSON. So is there a way to create the following ?:</p>
  
  <pre><code>{45: {"50492":{"Annual Expenditure":1000,"Annual Frequency":1,"Avg days":null,"First visit":1486339200000,"Frequency":1,"Last visit":1486339200000,"Merchants":45,"Monetory":1000,"Recency":270,"Visit_Ids":[300758],"RFMClass":"144","AvgLTV":113800.0},"1041":{"Annual Expenditure":1000,"Annual Frequency":1,"Avg days":null,"First visit":1478649600000,"Frequency":1,"Last visit":1478649600000,"Merchants":45,"Monetory":1000,"Recency":359,"Visit_Ids":[257022],"RFMClass":"244","AvgLTV":113800.0},"783":{"Annual Expenditure":1000,"Annual Frequency":1,"Avg days":null,"First visit":1457049600000,"Frequency":1,"Last visit":1457049600000,"Merchants":45,"Monetory":1000,"Recency":609,"Visit_Ids":[123464],"RFMClass":"444","AvgLTV":113800.0}}}
  </code></pre>
  
  <p>Here the 45 is a unique identifier that I'll pass in at the end of the loop.</p>