🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.to_sql()

AI & DATA SCIENCE // df-to-sql

df.to_sql() writes a DataFrame's rows into a database table, creating the table automatically if it doesn't already exist.

Syntax

df.to_sql(name, con, if_exists='fail', index=True)

Deep Dive Course

to_sql() infers appropriate SQL column types from the DataFrame's dtypes and either creates a brand-new table or writes into an existing one, depending on the if_exists parameter: 'fail', the default, raises an error if the table already exists, 'replace' drops and recreates it entirely, and 'append' adds the new rows to whatever's already there without touching existing data. Like to_csv()/to_excel(), it includes the DataFrame's index as a column by default unless you pass index=False.

1Understanding df.to_sql()

to_sql() infers appropriate SQL column types from the DataFrame's dtypes and either creates a brand-new table or writes into an existing one, depending on the if_exists parameter: 'fail', the default, raises an error if the table already exists, 'replace' drops and recreates it entirely, and 'append' adds the new rows to whatever's already there without touching existing data. Like to_csv()/to_excel(), it includes the DataFrame's index as a column by default unless you pass index=False.

💡

Double-check if_exists carefully before writing to a table that already has important data — 'replace' completely drops and recreates the table, permanently discarding whatever was in it before, which is easy to do by accident if you were expecting 'append' behavior.

editor.html
import pandas as pd
import sqlite3

conn = sqlite3.connect("shop.db")
df = pd.DataFrame({"product": ["Widget"], "price": [9.99]})
df.to_sql("products", conn, if_exists="replace", index=False)
print(pd.read_sql("SELECT * FROM products", conn))
localhost:3000

2Practical Example

Here is a real-world application of df.to_sql() showing how it is used in production Pandas code.

editor.html
import pandas as pd
import sqlite3

conn = sqlite3.connect("shop.db")
new_orders = pd.DataFrame({"id": [101], "total": [59.99]})
new_orders.to_sql("orders", conn, if_exists="append", index=False)
localhost:3000

3Best Practices

Follow these guidelines when working with df.to_sql():

1. Choose if_exists deliberately based on intent — 'append' to add new rows, 'replace' only when you genuinely want to wipe and recreate the table, 'fail' as a safety default when you're not sure

2. Pass index=False unless the DataFrame's index specifically needs to become its own database column

3. Write large DataFrames in chunks, using the chunksize parameter, rather than one massive insert, to avoid excessive memory usage or transaction size on the database side

⚠️

Tip: Double-check if_exists carefully before writing to a table that already has important data — 'replace' completely drops and recreates the table, permanently discarding whatever was in it before, which is easy to do by accident if you were expecting 'append' behavior.

editor.html
import pandas as pd
import sqlite3

conn = sqlite3.connect("shop.db")
df = pd.DataFrame({"product": ["Widget"], "price": [9.99]})
df.to_sql("products", conn, if_exists="replace", index=False)
print(pd.read_sql("SELECT * FROM products", conn))
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd
import sqlite3

conn = sqlite3.connect("shop.db")
df = pd.DataFrame({"product": ["Widget"], "price": [9.99]})
df.to_sql("products", conn, if_exists="replace", index=False)
print(pd.read_sql("SELECT * FROM products", conn))
Example 02Advanced Example
import pandas as pd
import sqlite3

conn = sqlite3.connect("shop.db")
new_orders = pd.DataFrame({"id": [101], "total": [59.99]})
new_orders.to_sql("orders", conn, if_exists="append", index=False)

Best Practices

  • Choose if_exists deliberately based on intent — 'append' to add new rows, 'replace' only when you genuinely want to wipe and recreate the table, 'fail' as a safety default when you're not sure
  • Pass index=False unless the DataFrame's index specifically needs to become its own database column
  • Write large DataFrames in chunks, using the chunksize parameter, rather than one massive insert, to avoid excessive memory usage or transaction size on the database side

Interview Question

What's the difference between if_exists='replace' and if_exists='append' when calling to_sql() on a table that already exists?

Hint: Think about what happens to the table's existing data in each case.

'replace' drops the entire existing table first and creates a brand-new one from the DataFrame's current data, permanently discarding whatever rows were previously in that table — it's a destructive, full overwrite. 'append' leaves the existing table and its data completely untouched, and simply inserts the DataFrame's rows as additional new rows alongside what was already there. Choosing the wrong one is a common, costly mistake: using 'replace' when you meant 'append' silently wipes out existing data that might not be recoverable.

Exercises

MediumPractice using df.to_sql() in a real scenario.
View Solution
import pandas as pd
import sqlite3

conn = sqlite3.connect("shop.db")
df = pd.DataFrame({"product": ["Widget"], "price": [9.99]})
df.to_sql("products", conn, if_exists="replace", index=False)
print(pd.read_sql("SELECT * FROM products", conn))

Frequently Asked Questions

What's the difference between if_exists='replace' and if_exists='append' when calling to_sql() on a table that already exists?

'replace' drops the entire existing table first and creates a brand-new one from the DataFrame's current data, permanently discarding whatever rows were previously in that table — it's a destructive, full overwrite. 'append' leaves the existing table and its data completely untouched, and simply inserts the DataFrame's rows as additional new rows alongside what was already there. Choosing the wrong one is a common, costly mistake: using 'replace' when you meant 'append' silently wipes out existing data that might not be recoverable.

Related Functions

pd-read-sqldf-to-csvsqlite3-module