Pandas 读取Mongodb中的数据

in PandasPython with 0 comment
# coding=utf-8
from pymongo import MongoClient  # 需要pip安装
import pandas as pd
 
 
client = MongoClient('192.168.0.11', 27017)  # 默认localhost,27017
collection = client["数据库名"]["集合名"]
data = collection.find()
data = list(data)  # 在转换成列表时,可以根据情况只过滤出需要的数据。(for遍历过滤)
 
df = pd.DataFrame(data)  # 读取整张表 (DataFrame)
print(df)
'''
                        _id  name  age
0  59ba7f9b421aa91b08a43faa  张三   18
1  59ba7f9b421aa91b08a43fab  李四   20

demo.py(pandas读取MongoDB,Series读取一条记录):

# coding=utf-8
from pymongo import MongoClient  # 需要pip安装
import pandas as pd
 
 
client = MongoClient('192.168.0.11', 27017)  # 默认localhost,27017
collection = client["数据库名"]["集合名"]
data = collection.find()
data = list(data)
 
t1 = data[0]  # 一条一条记录地读 (Series)
t1 = pd.Series(t1)
print(t1)
'''
_id       59ba7f9b421aa91b08a43faa
name      张三
age       18
'''
Responses