</>
Skip to content
Pandas lessons (37/42)

Pandas — SQL

Read from SQL

import pandas as pd
import sqlite3

conn = sqlite3.connect('database.db')
df = pd.read_sql('SELECT * FROM users', conn)

Write to SQL

df.to_sql('users', conn, if_exists='replace', index=False)

SQLAlchemy

from sqlalchemy import create_engine

engine = create_engine('postgresql://user:pass@localhost/db')
df = pd.read_sql('SELECT * FROM users', engine)

Query with parameters

query = "SELECT * FROM users WHERE age > ?"
df = pd.read_sql(query, conn, params=[25])

Mini Practice

  1. Connect to database
  2. Read data
  3. Write data
  4. Use parameters

Up Next

Continue with Selection - Selecting data.

Related Topics

Frequently Asked Questions about SQL

What is SQL in Pandas?

SQL is a fundamental concept in Pandas. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn SQL?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn SQL.

Why is SQL important in Pandas?

SQL is essential for Pandas development. Understanding this concept will help you write better code and solve real-world problems more effectively.