How to SAVE a Pandas DataFrame into an SQLite Database using Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Databases are extremely nice, especially when you want to handle a large amount of structured data.
Here is how to save a pandas DataFrame in an SQLite database using the DataFrame.to_sql() methods.
Here is the code
How to write into a SQLite Database
Here we write an example into the database.sqlite. In SQLite, a database is literally a file.
import pandas as pd
import sqlite3
# Create the connection to the database
con = sqlite3.connect("database.sqlite")
# We execute the sql query against the database
df = pd.DataFrame({"col1": range(10)})
# We print the dataframe
df.to_sql("my_table", con)
# Verify that result of SQL query is stored in the dataframe
print(df.head())
# We close the connection
con.close()
Here you are! You now know how to save a Pandas DataFrame into an SQLite Database.
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚