to_json and to_dict have different orient options

388 views
Skip to first unread message

rl...@cam.ac.uk

unread,
Feb 15, 2015, 10:43:00 AM2/15/15
to pyd...@googlegroups.com
Dear all,

I am attempting to serialise some data frames as JSON. i needed to add more stuff to the JSON object, so I thought I would just create the data structure with python dicts then serialize it all with the built in json library.  However, there is no 'index' orient for to_dict, which was what I needed.  It is easy to work around, but seems like there should just be identical arguments and options for to_json and to_dict, as JSON and python dicts are very similar.

Example of what I wanted:

In [48]: df1.to_json(orient='index')

Out[48]: '{"1":{"x":-3.0596136316,"y":3.9647826165},"2":{"x":-0.1949901632,"y":6.7491740324},"3":{"x":-4.6255730618,"y":-6.1347233236},"4":{"x":0.8868686424,"y":0.6795738338},"5":{"x":3.2204563871,"y":7.2071757778},"6":{"x":-1.5092076128,"y":-3.0109973696},"7":{"x":6.3871189241,"y":1.6910217563},"8":{"x":6.3871189241,"y":1.6910217563},"9":{"x":-3.3312500961,"y":0.1959806323},"10":{"x":0.5893350317,"y":-6.1787819037},"11":{"x":2.3357090154,"y":-2.4205021482},"12":{"x":6.7582596085,"y":-6.2947393629},"13":{"x":-5.6061536186,"y":-2.2074371945},"14":{"x":-8.2380783492,"y":4.0684508971}}'


In [49]: df1.to_dict(orient='index')

---------------------------------------------------------------------------

ValueError                                Traceback (most recent call last)

<ipython-input-49-db857a7f7faa> in <module>()

----> 1 df1.to_dict(orient='index')


/Users/RichLewis/anaconda/lib/python2.7/site-packages/pandas/util/decorators.pyc in wrapper(*args, **kwargs)

     86                 else:

     87                     kwargs[new_arg_name] = new_arg_value

---> 88             return func(*args, **kwargs)

     89         return wrapper

     90     return _deprecate_kwarg


/Users/RichLewis/anaconda/lib/python2.7/site-packages/pandas/core/frame.pyc in to_dict(self, orient)

    710                     for row in self.values]

    711         else:

--> 712             raise ValueError("orient '%s' not understood" % orient)

    713 

    714     def to_gbq(self, destination_table, project_id=None, chunksize=10000,


ValueError: orient 'index' not understood


In [50]: json.loads(df1.to_json(orient='index'))

Out[50]: 

{u'1': {u'x': -3.0596136316, u'y': 3.9647826165},

 u'10': {u'x': 0.5893350317, u'y': -6.1787819037},

 u'11': {u'x': 2.3357090154, u'y': -2.4205021482},

 u'12': {u'x': 6.7582596085, u'y': -6.2947393629},

 u'13': {u'x': -5.6061536186, u'y': -2.2074371945},

 u'14': {u'x': -8.2380783492, u'y': 4.0684508971},

 u'2': {u'x': -0.1949901632, u'y': 6.7491740324},

 u'3': {u'x': -4.6255730618, u'y': -6.1347233236},

 u'4': {u'x': 0.8868686424, u'y': 0.6795738338},

 u'5': {u'x': 3.2204563871, u'y': 7.2071757778},

 u'6': {u'x': -1.5092076128, u'y': -3.0109973696},

 u'7': {u'x': 6.3871189241, u'y': 1.6910217563},

 u'8': {u'x': 6.3871189241, u'y': 1.6910217563},

 u'9': {u'x': -3.3312500961, u'y': 0.1959806323}}


I'm relatively new to pandas, is my approach reasonable, or am I missing a trick?

Thanks,
Rich
Reply all
Reply to author
Forward
0 new messages