24. How to Load SQL Query Results into a pandas DataFrame
24.7 Practice, Tip and Close
Why. Connection + query + DataFrame must feel boringly repeatable before interview day (Chapter 18) or project week (Chapter 16 style refreshes).
What. One lab: build SQLite orders, pull with parameters, summarise in pandas, export a tiny CSV (Chapter 22 skill).
How.
Chapter practice lab
- Recreate
data/blinkit_lab.dbwith at least 12 fictional orders across the six Maharashtra cities (include Cancelled / Returned). - Connect with
sqlite3and once with SQLAlchemycreate_engine. - Pull Delivered rows for Pune and Nashik using
params=(no f-string values). - In pandas, compute orders count and GMV by city; print median basket.
- Export
outputs/sql_pull_city_gmv.csvwithindex=False. - Write three bullets: why SQL filter, why params, what
shapeyou got.
import sqlite3
from pathlib import Path
import pandas as pd
from sqlalchemy import create_engine
Path("data").mkdir(exist_ok=True)
Path("outputs").mkdir(exist_ok=True)
# If you already ran 24.3, you can skip recreate — or rebuild for a clean lab.
con = sqlite3.connect("data/blinkit_lab.db")
df = pd.read_sql_query(
"""
SELECT city, amount_inr
FROM orders
WHERE status = ? AND city IN (?, ?)
""",
con,
params=("Delivered", "Pune", "Nashik"),
)
con.close()
summary = (
df.groupby("city", as_index=False)["amount_inr"]
.agg(orders="count", gmv="sum", median_basket="median")
)
summary.to_csv("outputs/sql_pull_city_gmv.csv", index=False)
print(summary)
engine = create_engine("sqlite:///data/blinkit_lab.db")
smoke = pd.read_sql_query("SELECT COUNT(*) AS n FROM orders", engine)
print(smoke)
engine.dispose()
Practice task
Amir asks: “Only Kolhapur + Solapur, Delivered, amount ≥ ₹100, parameterized.” Produce the DataFrame and a one-line WhatsApp: city counts + GMV. No production URL.
Ravindra Bagale's Tip
Use SQL to cut the data down, pandas to tell the story, and a CSV or PNG file to share the result. Your first real success is a small, correct pull, not a brave SELECT * on everything. Keep passwords out of Git at all times.
Ravindra Bagale's Tip – मराठी
Data कमी करण्यासाठी SQL, गोष्ट सांगण्यासाठी pandas, आणि निकाल share करण्यासाठी CSV किंवा PNG file वापरा. तुमचं पहिलं खरं यश म्हणजे छोटी आणि बरोबर pull, सगळ्या data वर धाडसाने चालवलेलं SELECT * नाही. Passwords कधीही Git मध्ये ठेवू नका.
Ravindra Bagale's Tip – हिंदी
Data कम करने के लिए SQL, कहानी बताने के लिए pandas, और result share करने के लिए CSV या PNG file इस्तेमाल करो। आपकी पहली असली सफलता एक छोटी और सही pull है, सारे data पर हिम्मत से चलाया गया SELECT * नहीं। Passwords कभी भी Git में मत रखो।
Thodkyaat sangaycha tar (quick recap)
- SQL holds live tables; pandas is your analysis counter
- Prefer read-only users; start with SQLite for class
read_sql_query→ normal DataFrame- SQLAlchemy URLs make MySQL / Postgres look similar
- Filter early in SQL on big tables; explore in pandas on extracts
- Use
params=— avoid f-string values; never load huge tables blind
Samajla ka? Aata SQL result seedha DataFrame madhe yenar — pudhe practice regular theva, lab DB var ek query/day.