Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dictionary of Pandas' Dataframe to JSON

As the question asks I have a dictionary of pandas' dataframes that I want to save so that I don't have to resample the data next time I start the ipython notebook. I tried something simple which has worked in other cases before:

import json
with open('result.json', 'w') as fp:
    json.dump(d, fp)

But I got this error:

[1001 rows x 6 columns] is not JSON serializable

I think this has something to do with my pandas dataframe, but any help would be much appreciated.

like image 792
qwertylpc Avatar asked Oct 11 '15 03:10

qwertylpc


1 Answers

You need to extend the JSON encoder so it knows how to serialise a dataframe. Example (using to_json method):

import json
class JSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if hasattr(obj, 'to_json'):
            return obj.to_json(orient='records')
        return json.JSONEncoder.default(self, obj)

Saving:

with open('result.json', 'w') as fp:
    json.dump({'1':df,'2':df}, fp, cls=JSONEncoder)

Now if you will do

json.load(open('result.json')

You will get a dictionary with your dataframes. You can load them using

pd.read_json(json.load(open('result.json'))['1'])
like image 118
hellpanderr Avatar answered Oct 18 '22 23:10

hellpanderr