惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
About on SuperTechFans
C
Cybersecurity and Infrastructure Security Agency CISA
N
News and Events Feed by Topic
C
Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
A
Arctic Wolf
Scott Helme
Scott Helme
P
Palo Alto Networks Blog
S
Schneier on Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tor Project blog
量子位
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
NISL@THU
NISL@THU
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
AWS News Blog
AWS News Blog
爱范儿
爱范儿
Last Week in AI
Last Week in AI
Y
Y Combinator Blog
L
LINUX DO - 最新话题
Security Archives - TechRepublic
Security Archives - TechRepublic
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Secure Thoughts
Cloudbric
Cloudbric
aimingoo的专栏
aimingoo的专栏
L
Lohrmann on Cybersecurity
TaoSecurity Blog
TaoSecurity Blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Hacker News: Ask HN
Hacker News: Ask HN
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The GitHub Blog
The GitHub Blog
有赞技术团队
有赞技术团队
S
Security @ Cisco Blogs
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
G
GRAHAM CLULEY
P
Proofpoint News Feed
V
V2EX
Martin Fowler
Martin Fowler
C
CERT Recently Published Vulnerability Notes
Attack and Defense Labs
Attack and Defense Labs
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
SecWiki News
SecWiki News
罗磊的独立博客
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
小众软件
小众软件
The Last Watchdog
The Last Watchdog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant Common SOC 2 Failures (Real World) Stop Vibe-Checking Your AI App: A Practical Guide to Evals How to Use SonarQube and SonarScanner Locally to Level Up Your Code Quality Your Next To-Do App Is Dead — I Replaced Mine with an OpenClaw AI Sign a Nostr event in 60 lines of Python using coincurve — no nostr-sdk, no nbxplorer, no rust toolchain ITGC Audit Explained Like You’re in Big 4 Patch Tuesday abril 2026: Microsoft parcha 163 vulnerabilidades y un zero-day en SharePoint Stop scraping everything: a better way to track competitor price changes Listing on MCPize + the Official MCP Registry while routing payments OUTSIDE the marketplace — how I kept 100% of my x402 revenue Building an AI-Powered Risk Intelligence System Using Serverless Architecture Why We Ripped Function Overloading Out of Our AI Toolchain Testing AI-Generated Code: How to Actually Know If It Works SaaS Churn Is Killing Your Business. Here Is What to Do About It (Without a Support Team) The Speed of AI Is No Longer Linear - And Self-Improving Models Are Why How to Implement RBAC for MCP Tools: A Practical Guide for Engineering Teams From Standard Quote to Persuasive Proposal: AI Automation for Arborists I built a CLI that scaffolds complete multi-tenant SaaS apps Axios CVE-2025–62718: The Silent SSRF Bug That Could Be Hiding in Your Node.js App Right Now The dashboard that ended our friendship Data Pipelines Explained Simply (and How to Build Them with Python) The Hidden Cost of AI Systems Nobody Talks About. undefined vs undeclared, and how typeof behaves Switching from file-based jobs to NATS/Kafka in Rust without changing code io_uring Adventures: Rust Servers That Love Syscalls Why Agentic AI is Killing the Traditional Database The POUR principles of web accessibility for developers and designers Quantum Neural Network 3D — A Deep Dive into Interactive WebGL Visualization How To Install Caveman In Codex On macOS And Windows Automation Pipeline Reliability: Why Your Workflow Breaks When Nobody Is Watching I Built an 'Open World' AI Coding Agent — It Works From ANY Folder From Freelancing to Product: A Tech Service Company's SaaS Transformation China's AI Giants: Adding Tencent Hunyuan & ByteDance Doubao to AI University (74 Providers) On the Vibe Coders and Their Lies clerk: Auto-Summarize Your Claude Code Sessions AI Weekly — 2026/04/10–04/17 | The Model Lockdown Is Here, but the Toolchain Is the Real Battleground AI 週報 — 2026/04/10–2026/04/17 模型封鎖潮來了,但工具鏈才是真戰場 Maybe this is how Open-Source apps are born... 🚀 Fine-Tune LLMs with LoRA and QLoRA: 2026 Guide tRPC v11 + Next.js App Router: End-to-End Type Safety Without the Boilerplate ShadCN UI in 2026: Why I Stopped Installing Component Libraries and Started Owning My Components SaaS Billing in React Server Components: Stripe + Supabase Without a Single `useEffect` Join our DEV Weekend Challenge — $1,000 in Prizes Across TEN winners! Submissions Due April 20 at 6:59 AM UTC. Implementing FSRS Spaced Repetition in Flutter + Supabase — Adding Memory Science to an AI Learning App "I Texted My Localhost From the Train — Claude Code Fixed the Bug Before I Got Home" I Built a Sales Prep AI and It Went Deeper Than Expected Design to Code #2: One JSON, Eleven Outputs Solving the 100M-Row Problem: A Summary Table Pattern for High-Volume Push Notification Logs Flutter Web With Wasm: What Actually Changes For Developers I Built 50 Royalty-Free Soundtracks for My Side Project in a Weekend Using AI Music Generation The Vibe Coding Security Checklist: 7 Things to Check Before You Ship Stop Letting Googlebot Guess Fix Your React App's SEO Right Desconstruindo o Streaming do LinkedIn: Como Criar um Engine de Extração de Vídeo de Alta Performance com HLS e FFmpeg (EDA Part-1) EDA (Exploratory Data Analysis) Explained With Real Life — Why Looking at Your Data Is the Most Important Step in Machine Learning Brand Relationship Management at Scale: Our 4-Touch Outreach System for 200+ Brands Why String.fromEnvironment() Might Return an Empty String in Dart JGuardrails 1.0.0 — Hardening Java LLM Apps Against Jailbreaks, Toxicity, and Prompt Injection Plan and Schedule a Full Week of Threads Content From One Claude Conversation Coding Cat Oran Ep3, Five Tables Changed Everything Updated: BFF Pattern I'm done watching freelancers get buried by 200 proposals. So I'm building the alternative. This is my first post BFS Algorithm in Java Step by Step Tutorial with Examples Tracking LLM Pricing Monthly: An Open Dataset for 22 AI Models How We Measure Content ROI on a Comparison Site: Revenue Attribution Without Perfect Data Introducing Nova AI Ops: The AI-Native Operating System for SRE Teams I built a free desktop video downloader for Windows — Grabbit How Talkie OCR Helps Vision-Impaired & Dyslexic Users Read the World Around Them VRCFaceTracking安装和iPhone面捕配置教程,有bug Even CrowdStrike Can't See Your Agents The Automation Gold Rush: What n8n Workflows and Claude Are Opening Up for Developers Right Now
Python Meets SQL: Pandas and Databases Together
Akhilesh · 2026-05-02 · via DEV Community

Every skill you have built across four phases comes together here.

Python for logic and transformation. Pandas for analysis. SQL for querying databases. They are not separate tools. In real data work they form a single pipeline. SQL pulls data from the database efficiently. Pandas transforms and analyzes it in memory. Python orchestrates the whole thing.

This post is about connecting them properly, doing it safely, and building the patterns you will use in every data project going forward.


Three Ways to Connect Python to a Database

SQLite lives inside Python's standard library. No installation. No server. The database is a single file. Perfect for learning, prototyping, and small applications.

import sqlite3
conn = sqlite3.connect("mydata.db")        # file on disk
conn = sqlite3.connect(":memory:")         # lives in RAM, gone when script ends

Enter fullscreen mode Exit fullscreen mode

SQLAlchemy is the universal database connector for Python. One interface, every database. PostgreSQL, MySQL, SQLite, Oracle, SQL Server, all use the same API.

pip install sqlalchemy psycopg2-binary     # for PostgreSQL
pip install sqlalchemy pymysql             # for MySQL

Enter fullscreen mode Exit fullscreen mode

from sqlalchemy import create_engine

sqlite_engine   = create_engine("sqlite:///mydata.db")
postgres_engine = create_engine("postgresql://user:password@localhost:5432/dbname")
mysql_engine    = create_engine("mysql+pymysql://user:password@localhost:3306/dbname")

Enter fullscreen mode Exit fullscreen mode

pandas.read_sql does not care which database you use as long as you pass a valid connection. This is the function that bridges SQL queries directly into DataFrames.


The Full Setup

import sqlite3
import pandas as pd
import numpy as np
from sqlalchemy import create_engine, text

engine = create_engine("sqlite:///analytics.db")

with engine.connect() as conn:
    conn.execute(text("""
        CREATE TABLE IF NOT EXISTS customers (
            customer_id  INTEGER PRIMARY KEY,
            name         TEXT,
            city         TEXT,
            segment      TEXT,
            created_date TEXT
        )
    """))

    conn.execute(text("""
        CREATE TABLE IF NOT EXISTS transactions (
            txn_id       INTEGER PRIMARY KEY,
            customer_id  INTEGER,
            amount       REAL,
            category     TEXT,
            txn_date     TEXT,
            status       TEXT
        )
    """))
    conn.commit()

customers_data = pd.DataFrame({
    "customer_id":  range(1, 9),
    "name":         ["Alex", "Priya", "Sam", "Jordan", "Lisa", "Ravi", "Tom", "Nina"],
    "city":         ["Mumbai", "Delhi", "Bangalore", "Mumbai", "Chennai", "Delhi", "Bangalore", "Mumbai"],
    "segment":      ["Premium", "Standard", "Standard", "Premium", "Standard", "Premium", "Standard", "Premium"],
    "created_date": ["2022-01-10", "2021-06-15", "2023-03-01", "2020-11-22",
                     "2022-08-30", "2021-02-14", "2023-07-05", "2021-09-18"]
})

np.random.seed(42)
n = 50
transactions_data = pd.DataFrame({
    "txn_id":      range(1, n + 1),
    "customer_id": np.random.randint(1, 9, n),
    "amount":      np.random.uniform(500, 80000, n).round(0),
    "category":    np.random.choice(["Electronics", "Clothing", "Food", "Travel"], n),
    "txn_date":    pd.date_range("2024-01-01", periods=n, freq="3D").strftime("%Y-%m-%d"),
    "status":      np.random.choice(["completed", "completed", "completed", "cancelled"], n)
})

customers_data.to_sql("customers", engine, if_exists="replace", index=False)
transactions_data.to_sql("transactions", engine, if_exists="replace", index=False)

print("Data loaded successfully.")
print(f"Customers: {len(customers_data)} rows")
print(f"Transactions: {len(transactions_data)} rows")

Enter fullscreen mode Exit fullscreen mode

to_sql writes a DataFrame directly to a database table. if_exists="replace" drops and recreates the table. if_exists="append" adds rows to an existing table. if_exists="fail" raises an error if the table exists.

index=False prevents Pandas from writing the DataFrame index as an extra column. Almost always what you want.


Reading Data: pd.read_sql_query

df = pd.read_sql_query(
    "SELECT * FROM customers",
    engine
)
print(df)

Enter fullscreen mode Exit fullscreen mode

Simple. Pass any SQL string and a connection or engine. Get a DataFrame back.

Parameterized queries with SQLAlchemy:

city = "Mumbai"

df = pd.read_sql_query(
    text("SELECT * FROM customers WHERE city = :city"),
    engine,
    params={"city": city}
)
print(df)

Enter fullscreen mode Exit fullscreen mode

Never use f-strings to build SQL queries with user input. This is how SQL injection attacks happen. Always use parameterized queries when values come from outside your code.

# WRONG - SQL injection risk
city = "Mumbai"
df = pd.read_sql_query(f"SELECT * FROM customers WHERE city = '{city}'", engine)

# CORRECT - parameterized
df = pd.read_sql_query(
    text("SELECT * FROM customers WHERE city = :city"),
    engine,
    params={"city": city}
)

Enter fullscreen mode Exit fullscreen mode


The Pipeline Pattern: SQL Filters, Pandas Transforms

The most common real-world pattern. Use SQL to pull only what you need. Use Pandas for everything else.

df = pd.read_sql_query("""
    SELECT
        c.customer_id,
        c.name,
        c.city,
        c.segment,
        COUNT(t.txn_id)       AS total_transactions,
        SUM(t.amount)         AS total_spent,
        AVG(t.amount)         AS avg_transaction,
        MAX(t.txn_date)       AS last_transaction
    FROM customers c
    LEFT JOIN transactions t
        ON c.customer_id = t.customer_id
        AND t.status = 'completed'
    GROUP BY c.customer_id, c.name, c.city, c.segment
    ORDER BY total_spent DESC NULLS LAST
""", engine)

df["last_transaction"] = pd.to_datetime(df["last_transaction"])
df["days_since_last"]  = (pd.Timestamp.now() - df["last_transaction"]).dt.days
df["customer_value"]   = pd.cut(
    df["total_spent"].fillna(0),
    bins=[0, 10000, 50000, float("inf")],
    labels=["Low", "Medium", "High"]
)

print(df[["name", "segment", "total_spent", "avg_transaction", "days_since_last", "customer_value"]])

Enter fullscreen mode Exit fullscreen mode

Output:

    name   segment  total_spent  avg_transaction  days_since_last customer_value
0   Alex   Premium     238500.0         29812.5             24.0           High
1  Priya  Standard     198000.0         28285.7             15.0           High
...

Enter fullscreen mode Exit fullscreen mode

SQL handled the aggregation across 50 rows of transaction data before it even touched Python. Pandas then added date calculations and customer value tiers that SQL could not do as cleanly.


Writing Results Back to the Database

Analysis results that others need should live in the database, not in your laptop's memory.

summary = df[["customer_id", "total_spent", "total_transactions", "customer_value", "days_since_last"]].copy()
summary["calculated_at"] = pd.Timestamp.now().strftime("%Y-%m-%d %H:%M:%S")

summary.to_sql(
    "customer_summary",
    engine,
    if_exists="replace",
    index=False
)

verification = pd.read_sql_query("SELECT * FROM customer_summary", engine)
print(f"Summary table written: {len(verification)} rows")
print(verification.head(3))

Enter fullscreen mode Exit fullscreen mode

The summary is now queryable by anyone with database access. A dashboard tool can read it. Another analyst can join it. An application can serve it to users.


Chunked Reading for Large Tables

When a table has millions of rows, loading it all into memory crashes things. Read it in chunks.

chunk_size = 1000
total_revenue = 0
row_count = 0

for chunk in pd.read_sql_query(
    "SELECT amount, status FROM transactions",
    engine,
    chunksize=chunk_size
):
    completed = chunk[chunk["status"] == "completed"]
    total_revenue += completed["amount"].sum()
    row_count += len(completed)

print(f"Processed {row_count} completed transactions")
print(f"Total revenue: {total_revenue:,.0f}")

Enter fullscreen mode Exit fullscreen mode

Each chunk is a DataFrame of chunk_size rows. Process it, extract what you need, let it get garbage-collected. You never hold more than one chunk in memory at once.


Context Managers and Connection Safety

Database connections are resources. Always close them properly. Context managers guarantee this.

with engine.connect() as conn:
    result = conn.execute(text("SELECT COUNT(*) FROM transactions"))
    count = result.scalar()
    print(f"Transaction count: {count}")

with sqlite3.connect("analytics.db") as conn:
    cursor = conn.cursor()
    cursor.execute("SELECT COUNT(*) FROM customers")
    count = cursor.fetchone()[0]
    print(f"Customer count: {count}")

Enter fullscreen mode Exit fullscreen mode

When the with block ends, the connection closes automatically. Even if an exception is raised inside the block. Never rely on garbage collection to close database connections. Always use context managers.


Transactions: All or Nothing

When you write multiple related records, they should either all succeed or all fail together. A transaction guarantees this.

def add_customer_with_transaction(engine, customer_data, initial_transaction):
    with engine.begin() as conn:
        conn.execute(
            text("""
                INSERT INTO customers (customer_id, name, city, segment, created_date)
                VALUES (:id, :name, :city, :segment, :date)
            """),
            {"id": customer_data["id"], "name": customer_data["name"],
             "city": customer_data["city"], "segment": customer_data["segment"],
             "date": customer_data["date"]}
        )

        conn.execute(
            text("""
                INSERT INTO transactions (txn_id, customer_id, amount, category, txn_date, status)
                VALUES (:txn_id, :customer_id, :amount, :category, :date, 'completed')
            """),
            {"txn_id": initial_transaction["txn_id"],
             "customer_id": customer_data["id"],
             "amount": initial_transaction["amount"],
             "category": initial_transaction["category"],
             "date": initial_transaction["date"]}
        )

    print(f"Customer {customer_data['name']} and first transaction added successfully.")

new_customer = {"id": 9, "name": "Oscar", "city": "Pune",
                "segment": "Standard", "date": "2024-03-01"}
first_txn    = {"txn_id": 51, "amount": 15000, "category": "Electronics", "date": "2024-03-01"}

add_customer_with_transaction(engine, new_customer, first_txn)

Enter fullscreen mode Exit fullscreen mode

engine.begin() starts a transaction. If any statement inside raises an exception, all changes are rolled back. The customer and the transaction are inserted together or not at all. No orphaned records.


A Complete Data Pipeline

Putting everything together. Extract from database, transform in Pandas, load back.

def run_etl_pipeline(engine):
    print("Step 1: Extract")
    raw = pd.read_sql_query("""
        SELECT
            t.txn_id, t.customer_id, t.amount, t.category,
            t.txn_date, t.status, c.city, c.segment
        FROM transactions t
        JOIN customers c ON t.customer_id = c.customer_id
        WHERE t.status = 'completed'
    """, engine)
    print(f"  Extracted {len(raw)} rows")

    print("Step 2: Transform")
    raw["txn_date"]  = pd.to_datetime(raw["txn_date"])
    raw["month"]     = raw["txn_date"].dt.to_period("M").astype(str)
    raw["amount_usd"] = (raw["amount"] / 83).round(2)

    monthly_summary = raw.groupby(["month", "category", "segment"]).agg(
        transactions=("txn_id", "count"),
        revenue=("amount", "sum"),
        avg_value=("amount", "mean")
    ).reset_index()
    monthly_summary["avg_value"] = monthly_summary["avg_value"].round(0)
    print(f"  Transformed to {len(monthly_summary)} summary rows")

    print("Step 3: Load")
    monthly_summary.to_sql("monthly_summary", engine, if_exists="replace", index=False)
    print("  Loaded to monthly_summary table")

    result = pd.read_sql_query(
        "SELECT * FROM monthly_summary ORDER BY month, revenue DESC",
        engine
    )
    print("\nFinal output:")
    print(result.head(10))

run_etl_pipeline(engine)

Enter fullscreen mode Exit fullscreen mode

Extract, transform, load. Every data pipeline you will ever build follows this shape. The tools change. The databases change. The pattern does not.


A Resource Worth Knowing

Robin Moffatt wrote a widely-read piece called "Kafka, JDBC and the Problem of Schema Evolution" but his more practical content lives on the Confluent blog where he writes about database connectivity patterns in Python for real production systems. His writing on SQLAlchemy connection pooling and transaction management is some of the most practical content available. Search "Robin Moffatt SQLAlchemy Python production."

For SQLAlchemy specifically, the documentation at docs.sqlalchemy.org/en/14/core/tutorial.html is unusually good. The "Core Tutorial" section reads like a well-written blog post, not a reference manual. Worth reading alongside this post.


Try This

Create python_sql_practice.py.

Build a complete mini analytics system on the database from this post.

Part 1: Segmentation pipeline. Query all completed transactions. In Pandas, calculate each customer's total spending, transaction count, average transaction value, and days since last purchase. Write the result back to a table called customer_rfm.

Part 2: Category report. Using a single SQL query (no Pandas processing), get revenue by category and month. Use a window function to add a column showing each category's percentage of total monthly revenue. Load into a DataFrame and print.

Part 3: Anomaly detection. Read the transactions table. In Pandas, calculate z-scores for the amount column. Flag any transaction more than 2 standard deviations from the mean as an anomaly. Write these flagged transactions to a table called transaction_anomalies.

Part 4: Pipeline function. Wrap all three parts into a single run_analysis(engine) function. It should print progress messages, handle exceptions with try/except, and print a final summary showing how many rows were written to each output table.


Phase 4 Complete

Five posts. From basic SELECT statements to joining three tables, subqueries, CTEs, window functions, and full Python-SQL integration.

SQL is not a separate subject from data science. It is the first tool you use in almost every analysis. The data lives in a database. SQL is how you get it out.

Phase 5 next. Three posts covering Git, GitHub, Jupyter, and Google Colab. Short phase. High leverage. These are the tools that make your work reproducible, shareable, and professional.