about Numpy
Formatted data :
Binary(pickle.dumps(array, protocol=2))
, among array Namely ndarry Formatted array pickle.loads(result['numpy'])
about Pandas
Formatted data :
json.dumps(dataframe.to_dict('records'))
pd.DataFrame(json.loads(result['dataframe']))
import pymongo
import numpy as np
import pandas as pd
import json
import pickle
from bson.binary import Binary
# Connect to database
mongo_conn = pymongo.MongoClient(host='localhost', port=27017)
db = mongo_conn.get_database("test") # Specify database
coll = db.get_collection("people") # Get the set
# Structural data
array = np.random.random(size=(5, 10)) # To save numpy Format data
dataframe = pd.DataFrame(data=np.random.random(size=(10, 20))) # To save pandas dataframe Format data
config_dict = {
'a': True,
'b': False,
'c': 3,
'd': ["a", "b", 3],
}
# Start saving
coll.insert_one({
# Insert
'_id': ' test ',
'dataframe': json.dumps(dataframe.to_dict('records')),
"numpy": Binary(pickle.dumps(array, protocol=2)),
"config_dict": config_dict,
})
# Go back to
result = coll.find_one({
"_id": " test "})
recover_dataframe = pd.DataFrame(json.loads(result['dataframe']))
recover_array = pickle.loads(result['numpy'])
recover_config_dict = result['config_dict']