如果我们读取mongodb的数据不含嵌套数据,我们可以直接将其载入到Pandas的dataframe中:
import pymongo as pm
import pandas as pd
import numpy as np
import datetime as dt
client = pm.MongoClient('mongodb://user1:user1@127.0.0.1:27017')
db = client['my_db']
proection = {'name':1,'id_no':1, }
mongo_data = list(db['students'].find({}, proection))
df = pd.DataFrame(mongo_date)
如果我们读取mongodb的数据含有嵌套数据,我们需要先将所有嵌套的数据通过json_util工具先解析出来,具体代码如下:
import pymongo as pm
import pandas as pd
import numpy as np
import datetime as dt
from bson import json_util
from pandas.io.json import json_normalize
import json
client = pm.MongoClient('mongodb://user1:user1@127.0.0.1:27017')
db = client['my_db']
mongo_data = list(db['students'].find({}))
sanitized = json.loads(json_util.dumps(mongo_data))
normalized = json_normalize(sanitized)
df = pd.DataFrame(normalized)
print(df)
结果如下: