Getting data from table in database Getting data from table in database postgresql postgresql

Getting data from table in database


Pandas can load data from Postgres directly:

import psycopg2import pandas.io.sql as pdsqlconn = psycopg2.connect(...)the_frame = pdsql.read_frame("SELECT * FROM %s;" % name_of_table, conn)

If you have a recent pandas (>=0.14), you should use read_sql_query/table (read_frame is deprecated) with an sqlalchemy engine:

import pandas as pdimport sqlalchemyimport psycopg2engine = sqlalchemy.create_engine("postgresql+psycopg2://...")the_frame = pd.read_sql_query("SELECT * FROM %s;" % name_of_table, engine)the_frame = pd.read_sql_table(name_of_table, engine)


Here is an alternate method:

    # run sql code    result = conn.execute(sql)       # Insert to a dataframe    df = DataFrame(data=list(result), columns=result.keys())