對於Numpy
格式的數據:
Binary(pickle.dumps(array, protocol=2))
,其中array就是ndarry格式的數組pickle.loads(result['numpy'])
對於Pandas
格式的數據:
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
# 連接數據庫
mongo_conn = pymongo.MongoClient(host='localhost', port=27017)
db = mongo_conn.get_database("test") # 指定數據庫
coll = db.get_collection("people") # 獲得集合
# 構造數據
array = np.random.random(size=(5, 10)) # 要保存的numpy格式數據
dataframe = pd.DataFrame(data=np.random.random(size=(10, 20))) # 要保存的pandas dataframe格式數據
config_dict = {
'a': True,
'b': False,
'c': 3,
'd': ["a", "b", 3],
}
# 開始保存
coll.insert_one({
# 插入
'_id': '測試',
'dataframe': json.dumps(dataframe.to_dict('records')),
"numpy": Binary(pickle.dumps(array, protocol=2)),
"config_dict": config_dict,
})
# 復原回去
result = coll.find_one({
"_id": "測試"})
recover_dataframe = pd.DataFrame(json.loads(result['dataframe']))
recover_array = pickle.loads(result['numpy'])
recover_config_dict = result['config_dict']