pd 写入数据库,将数据框写入Postgres数据库

I want to write a pandas dataframe to a postgres table. I make a connection to db as follows:

import psycopg2

import pandas as pd

import sqlalchemy

def connect(user, password, db, host='localhost', port=5432):

'''Returns a connection and a metadata object'''

url = 'postgresql://{}:{}@{}:{}/{}'

url = url.format(user, password, host, port, db)

# The return value of create_engine() is our connection object

con = sqlalchemy.create_engine(url, client_encoding='utf8')

# We then bind the connection to MetaData()

meta = sqlalchemy.MetaData(bind=con, reflect=True)

return con, meta

con, meta = connect('user_name', 'password', 'db_name', host='host_name')

When I read from a table that is already populated, it works fine:

df = pd.read_sql("SELECT * FROM db.table_name limit 10",con=con)

print df

I would like to be able to write df to a table. To test this, I have a temporary table called 'test' with two fields name and age.

# create a temp df

table = [['name', 'age'], ['nameA' , 20], ['nameB', 30]]

headers = table.pop(0)

df = pd.DataFrame(table, columns=headers)

# write to db

df.to_sql('db.test', con, if_exists = 'replace', index=False)

I then check if the temp table is populated:

df = pd.read_sql("SELECT * FROM db.test limit 10",con=con)

print df

I get an empty dataframe! I got no errors when I use df.to_sql but nothing is getting written to the database (?). What am I missing and how do I go about fixing this?

Versions:

Pandas: 0.19.2

Sqlachemy: 1.1.10

Postgres: 9.4.9

解决方案

I have not figured out why df.to_sql did not write to the table. Writing to table using pd.io.sql.SQLDatabase worked for my test case:

meta = sqlalchemy.MetaData(con, schema='db_name')

meta.reflect()

pdsql = pd.io.sql.SQLDatabase(con, meta=meta)

pdsql.to_sql(df, 'test', if_exists='replace')

I would not consider this THE solution -- I'd be happy to accept better solution or an answer that brings a closure to why df.to_sql() does not behave as expected.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值