data-analysis-guidelines
data-analysis-guidelines copied to clipboard
Add basic setup for database-backed analysis
pip
install sqlalchemy
and psycopg2
.
Then, in your Pweave markdown (.pmd
) or Pweave TeX (.ptexw
) file:
Import pandas and sqlalchemy, and connect to your database.
<<>>=
import pandas as pd
from sqlalchemy import create_engine
DATABASE_FORMAT = 'postgresql://{user}:{pw}@{host}:{port}/{name}'
DATABASE_OPTIONS = {
'user': 'postgres',
'host': 'localhost',
'pw: '',
'port': 5432,
'name': 'YOUR DATABASE', # Change this to the name of your database
}
DATABASE_CONNECTION = DATABASE_FORMAT.format(**DATABASE_OPTIONS)
engine = create_engine(DATABASE_CONNECTION)
conn = engine.connect()
@
Issue SQL to your database with pandas.read_sql(). The first argument can be a SQL query, or the name of a table (if you want to select the entire thing). The second argument is your database connection.
<<>>=
pd.read_sql('SELECT * FROM my_table', conn) # Returns query result as pandas DataFrame.
@
At the very end of your file, close your connection.
<<>>=
conn.close()
@
Update: You can just pass the engine