In [ ]:
# 使用pgAdmin
from sqlalchemy import create_engine
import numpy as np
import pandas as pd
import sqlite3
import psycopg2
engine = create_engine('postgresql+psycopg2://postgres:password@localhost:54749/postgres')
frame = pd.DataFrame(np.random.random((4,4)),
                    index=['exp1','exp2','exp3','exp4'],
                    columns=['fed','mar','apr','may'])
frame.to_sql('dataframe', engine)

In [ ]:
# NoSQL数据MongoDB数据读写
import pymongo as pymg
client = pymg.MongoClient('localhost', 27017) # 连接数据库
db = client.mydatabase
db

In [ ]:
client['mydatabase'] # 指定一个数据库

In [ ]:
collection = db.mycollection
db['mycollection'] # 定义集合(collection)

In [ ]:
collection

In [ ]:
frame = pd.DataFrame(np.arange(20).reshape(4,5),
                    columns=['white','red','blue','black','green'])
frame
# 必须转换为JSON格式
import json
record = json.loads(frame.T.to_json()).values()
record
collection.mydocument.insert(record) # 保存数据

In [ ]:
cursor = collection['mydocument'].find()
dataframe = pd.DataFrame(list(cursor))
del dataframe['_id'] # 删除mognodb内部索引的id编号一列
dataframe

In [ ]: