Streamlined Data Ingestion with pandas
Amany Mahfouz
Instructor
sqlalchemy
's create_engine()
makes an engine to handle database connectionssqlite:///filename.db
pd.read_sql(query, engine)
to load in data from a databasequery
: String containing SQL query to run or table to loadengine
: Connection/database engine objectSELECT [column_names] FROM [table_name];
SELECT * FROM [table_name];
# Load pandas and sqlalchemy's create_engine import pandas as pd from sqlalchemy import create_engine
# Create database engine to manage connections engine = create_engine("sqlite:///data.db")
# Load entire weather table by table name weather = pd.read_sql("weather", engine)
# Create database engine to manage connections engine = create_engine("sqlite:///data.db")
# Load entire weather table with SQL weather = pd.read_sql("SELECT * FROM weather", engine)
print(weather.head())
station name latitude ... prcp snow tavg tmax tmin
0 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 52 42
1 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 48 39
2 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 48 42
3 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.00 0.0 51 40
4 USW00094728 NY CITY CENTRAL PARK, NY US 40.77898 ... 0.75 0.0 61 50
[5 rows x 13 columns]
Streamlined Data Ingestion with pandas