使用sqlalchemy从PostgreSQL查询返回Pandas数据帧

lma*_*999 52 python postgresql sqlalchemy pandas

我想查询PostgreSQL数据库并将输出作为Pandas数据帧返回.

我使用'SqlAlchemy'创建了与数据库的连接:

from sqlalchemy import create_engine
engine = create_engine('postgresql://user@localhost:5432/mydb')
Run Code Online (Sandbox Code Playgroud)

我将Pandas数据帧写入数据库表:

i=pd.read_csv(path)
i.to_sql('Stat_Table',engine,if_exists='replace')
Run Code Online (Sandbox Code Playgroud)

根据文档,看起来pd.read_sql_query()应该接受SQLAlchemy引擎:

a=pd.read_sql_query('select * from Stat_Table',con=engine)
Run Code Online (Sandbox Code Playgroud)

但它抛出一个错误:

ProgrammingError: (ProgrammingError) relation "stat_table" does not exist
Run Code Online (Sandbox Code Playgroud)

我正在使用Pandas版本0.14.1.

这样做的正确方法是什么?

jor*_*ris 64

您被PostgreSQL的案例(in)敏感性问题所困扰.如果在查询中引用表名,它将起作用:

df = pd.read_sql_query('select * from "Stat_Table"',con=engine)
Run Code Online (Sandbox Code Playgroud)

但就个人而言,我建议只使用小写表名(和列名),同时将表写入数据库以防止此类问题.


来自PostgreSQL文档(http://www.postgresql.org/docs/8.0/static/sql-syntax.html#SQL-SYNTAX-IDENTIFIERS):

引用标识符也会使其区分大小写,而不带引号的名称始终折叠为小写

To explain a bit more: you have written a table with the name Stat_Table to the database (and sqlalchemy will quote this name, so it will be written as "Stat_Table" in the postgres database). When doing the query 'select * from Stat_Table' the unquoted table name will be converted to lower case stat_table, and so you get the message that this table is not found.

See eg also Are PostgreSQL column names case-sensitive?


Use*_*716 21

迟到了,但给你一个完整的例子:

import pandas as pd
import psycopg2 as pg

engine = pg.connect("dbname='my_db_name' user='pguser' host='127.0.0.1' port='15432' password='pgpassword'")
df = pd.read_sql('select * from Stat_Table', con=engine)
Run Code Online (Sandbox Code Playgroud)

您需要运行以下命令来安装 ubuntu 的依赖项:

import pandas as pd
import psycopg2 as pg

engine = pg.connect("dbname='my_db_name' user='pguser' host='127.0.0.1' port='15432' password='pgpassword'")
df = pd.read_sql('select * from Stat_Table', con=engine)
Run Code Online (Sandbox Code Playgroud)

关于这个主题的 Pandas 文档在这里


Cha*_*ngh 8

在下面给出的熊猫中读取 postgres sql 数据和图像链接

import psycopg2 as pg
import pandas.io.sql as psql
connection = pg.connect("host=localhost dbname=kinder user=your_username password=your_password")
dataframe = psql.read_sql('SELECT * FROM product_product', connection)
product_category = psql.read_sql_query('select * from product_category', connection)
Run Code Online (Sandbox Code Playgroud)

https://i.stack.imgur.com/1bege.png