Connecting Pandas to a Database with SQLAlchemy
Last Updated :
26 Jan, 2022
In this article, we will discuss how to connect pandas to a database and perform database operations using SQLAlchemy.
The first step is to establish a connection with your existing database, using the create_engine() function of SQLAlchemy.
Syntax:
from sqlalchemy import create_engine
engine = create_engine(dialect+driver://username:password@host:port/database)
Explanation:
- dialect - Name of the DBMS
- driver - Name of the DB API that moves information between SQLAlchemy and the database.
- Username, Password - DB User credentials
- host: port - Specify the type of host and port number.
- Database - Database name
Connecting Pandas to a Database with SQLAlchemy
Syntax: pandas.DataFrame.to_sql(table_name, engine_name, if_exists, index)
Explanation:
- table_name - Name in which the table has to be stored
- engine_name - Name of the engine which is connected to the database
- if_exists - By default, pandas throws an error if the table_name already exists. Use 'REPLACE' to replace this dataset with the old one or "APPEND" to add the data to the existing table.
- index - (bool), Adds index column to the table that identifies each row uniquely.
For this example, we can use a PostgreSQL database, which is one of the easiest ways to do things, but then the procedure is just the same for all the other databases supported by SQLAlchemy. You can download the sample dataset here.
Let us first Import the necessary dataset. Now, let's Establish the connection with the PostgreSQL database and make it interactable to python using the psycopg2 driver. Next, we shall load the dataframe to be pushed to our SQLite database using the to_sql() function as shown.
Python3
# import necessary packages
import pandas
import psycopg2
from sqlalchemy import create_engine
# establish connection with the database
engine = create_engine(
"dialect+driver//username:password@hostname:portnumber/databasename")
# read the pandas dataframe
data = pandas.read_csv("path to dataset")
# connect the pandas dataframe with postgresql table
data.to_sql('loan_data', engine, if_exists='replace')
Output:
This will create a table named loan_data in the PostgreSQL database.
Connecting a table to PostgreSQL databaseConverting a PostgreSQL table to pandas dataframe
Like we did above, we can also convert a PostgreSQL table to a pandas dataframe using the read_sql_table() function as shown below. Here, let us read the loan_data table as shown below.
Syntax: pandas.DataFrame.read_sql_table(table_name, con = engine_name, columns)
Explanation:
- table_name - Name in which the table has to be stored
- con - Name of the engine which is connected to the database
- columns - list of columns that has to be read from the SQL table
Python3
# import necessary packages
import pandas as pd
import psycopg2
from sqlalchemy import create_engine
# establish connection with the database
engine = create_engine(
"dialect+driver//username:password@hostname:portnumber/databasename")
# read the postgresql table
table_df = pd.read_sql_table(
"loan_data",
con=engine,
columns=['Loan_ID',
'Gender',
'Married',
'Dependents',
'Education',
'Self_Employed',
'ApplicantIncome',
'CoapplicantIncome',
'LoanAmount',
'Loan_Amount_Term',
'Credit_History',
'Property_Area',
'Loan_Status'],
)
# print the postgresql table loaded as
# pandas dataframe
print(table_df)
Output:
Postgresql table read as a dataframe using SQLAlchemyPassing SQL queries to query table data
We can also pass SQL queries to the read_sql_table function to read-only specific columns or records from the PostgreSQL database. The procedure is still the same. The SQL syntax remains the same as a conventional syntax to query data from a SQL table. The below example shows how to get all records of loan_data table using SQL query.
Python3
# import necessary packages
import pandas as pd
import psycopg2
from sqlalchemy import create_engine
# establish connection with the database
engine = create_engine(
"dialect+driver//username:password@hostname:portnumber/databasename")
# read table data using sql query
sql_df = pd.read_sql(
"SELECT * FROM loan_data",
con=engine
)
print(sql_df)
Output:
Postgresql table read as a dataframe using SQLAlchemy
Similar Reads
Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read