Ravindra Bagale · Data Scienceसर्व coursesया course चे lessonsशोधाEnglish

Data Science · मराठी आवृत्ती

24.5 Filter SQL मध्ये vs pandas मध्ये

By Ravindra Bagale

या course चे सर्व chapters (py + ch)

हे का शिकायचं?

मोठ्या tables वर आधी SQL मध्ये filter — नेटवर्क आणि RAM वाचतो. छोटे result वर pandas filters.

हे काय आहे?

Rule of thumb: row cutdowns जवळ DB. Complex reshaping pandas मध्ये.

कसं करायचं?

Step 1. Date/city filters SQL WHERE मध्ये.

Step 2. Aggregate शक्य तो SQL.

Step 3. Final reshape pandas.

Step 4. दोन्हीकडे n log करा.

उदाहरणं: copy करा आणि चालवा

Situation Prefer
8-row lab DB Either SQL or pandas filter
Millions of rows, one city SQL WHERE
Same 50k extract, ten chart ideas Pull once; filter in pandas
Only city totals needed SQL GROUP BY then small DataFrame

उदाहरण A — Same answer, two places.

import sqlite3
import pandas as pd

con = sqlite3.connect("data/blinkit_lab.db")

sql_filtered = """
SELECT city, amount_inr
FROM orders
WHERE status = 'Delivered' AND city IN ('Pune', 'Nashik')
"""
df_sql = pd.read_sql_query(sql_filtered, con)

df_all = pd.read_sql_query(
    "SELECT city, status, amount_inr FROM orders",
    con,
)
con.close()

df_pd = df_all.loc[
    (df_all["status"] == "Delivered")
    & (df_all["city"].isin(["Pune", "Nashik"])),
    ["city", "amount_inr"],
]

print("SQL filter GMV", df_sql["amount_inr"].sum())
print("pandas filter GMV", df_pd["amount_inr"].sum())

उदाहरण B — Heavy table habit.

Zoya’s orders table has millions of rows. She never read_sql_table("orders"). She asks SQL for one festival week and six Maharashtra cities, then plots in seaborn.

उदाहरण C — Decision card.

Situation Prefer
8-row lab DB Either SQL or pandas filter
Millions of rows, one city SQL WHERE
Same 50k extract, ten chart ideas Pull once; filter in pandas
Only city totals needed SQL GROUP BY then small DataFrame