Dictionary of Pandas' Dataframe to JSON
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'])
While the above works, the serialized dataframes go into json as embedded strings. If you want pretty json, first convert dataframes into dictionaries, then write using normal json interface. You would convert back to dataframes after reading from disk:
# data is dictionary of dataframes
import json
# convert dataframes into dictionaries
data_dict = {
key: data[key].to_dict(orient='records')
for key in data.keys()
}
# write to disk
with open('data_dict.json', 'w') as fp:
json.dump(
data_dict,
fp,
indent=4,
sort_keys=True
)
# read from disk
with open('data_dict.json', 'r') as fp:
data_dict = json.load(fp)
# convert dictionaries into dataframes
data = {
key: pd.DataFrame(data_dict[key])
for key in data_dict
}