Python 如何在现有postgres sql数据库中插入数据帧?
我有一个这样的数据帧Python 如何在现有postgres sql数据库中插入数据帧?,python,sql,postgresql,dataframe,Python,Sql,Postgresql,Dataframe,我有一个这样的数据帧 index userID OtherIDs 0 abcdef2035 [test650, test447, test968, test95] 1 abcdef3007 [test999, test992, test943, test834] 2 abcdef2006 [test175, test996, test986, test965] 3 abcdef2003 [test339, test968, test87, test678] 4
index userID OtherIDs
0 abcdef2035 [test650, test447, test968, test95]
1 abcdef3007 [test999, test992, test943, test834]
2 abcdef2006 [test175, test996, test986, test965]
3 abcdef2003 [test339, test968, test87, test678]
4 abcdef3000 [test129, test99, test921, test909]
生成此数据帧的代码将每天运行。我需要将其上传到现有数据库中的表名“result”。我必须检查表“结果”是否存在,如果存在,请使用上述数据框中的当前值删除/覆盖这些值
博士后荣誉:
PGHOST = 'localhost'
PGDATABASE = 'TestDB'
PGUSER = 'postgres'
PGPASSWORD = 'admin1234'
您可以使用SQLAlchemy:()
熊猫df.to_sql:()
假设数据帧名称为df
from sqlalchemy import create_engine
engine = create_engine(user:password@host_ip:port/postgres_database)
df.to_sql('results', schema='<schema_name>', con = engine, if_exists='replace')
你试过了吗?请包括您当前的代码。还有,你为什么在这里添加用户名和密码?这不应该和你的问题有任何关系。
sign_in = {
"database": "TestDB",
"user": "postgres",
"password": "<your_password>",
"host": "localhost",
"port": "<your_port>"
}
signin_info = 'postgresql+pygresql://'+sign_in['user']+':'+sign_in['password']+'@'+sign_in['host']+':'+sign_in['port']+'/'+sign_in['database']
from sqlalchemy import create_engine
engine = create_engine(signin_info)
df.to_sql('results', schema='<schema_name>', con = engine, if_exists='replace')