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'])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…