Part 5.2 !! Pandas DataFrame to PostgreSQL using Python

Extract, Transform, and Load PostgreSQL Data in Python

Learner CARES
Analytics Vidhya
3 min readNov 30, 2020

--

Prerequisites

Python 3.8.3 : Anaconda download link

PostgreSQL 13 : Download link

Psycopg2 : To install Psycopg2 use the command: pip install psycopg2, petl

Objective

Generally we create training and testing data by importing csv file into pandas DataFrame but when we have large data stored in database server then we need a method to extract it into pandas DataFrame directly from database server. For this objective, we will learn step by step working code for different methods for doing it.

Step 1 : Import libraries

Step 2 : Specify the connection parameters

Step 3 : Support Functions

Step 4: Loading PostgreSQL Data into a CSV File : Extract, Transform, and Load the PostgreSQL Data

We can use petl to extract, transform, and load the PostgreSQL data. In this example, we extract PostgreSQL data, sort the data by the species column, and load the data into a CSV file.

Transform

Here, you can perform different opations with data after loading. For example, you can do the following

  1. join tables,
  2. sorting,
  3. fill missing values
  4. Reshaping tables etc.

Load (Write data)

After all operatons with data, now can save your data in you disk for reporting or later use.

Conclusion : This ends our Part 5.2. In this tutorial we have learned how to extract, transform and load data.

All code for this article is available as a Jupyter Notebook on GitHub.

Previous Learning :

Part 1: Introduction , Connection & Database Creation

Part 2 Create Table in PostgreSQL Database Using Python

Part 3.1: Insert Bulk Data Using executemany() Into PostgreSQL Database

Part 3.2: Insert Bulk Data Using execute_batch() Into PostgreSQL Database

Part 3.3: Insert Bulk Data Using execute_values() method into PostgreSQL Database using Python

Part 3.4: Insert Bulk Data Using mogrify() Into PostgreSQL Database

Part 3.5: Insert Bulk Data Using copy_from() Into PostgreSQL Database

Part 3.6 : Insert Bulk Data Using copy_from() with StringIO Into PostgreSQL Database

Part 3.7 : Insert Bulk Data Using to_sql()(alchemy) Into PostgreSQL Database

Part 4: Comparison of Methods for Importing bulk CSV data Into PostgreSQL Using Python

Part 5.1 : How to read data from PostgreSQL to Pandas DataFrame?

Reference: petl : petl — Extract, Transform and Load (Tables of Data) — petl 1.6.8 documentation

Stay Positive !! Stay Safe !! Keep Learning :):)

Thanks you for reading! !

--

--

Learner CARES
Analytics Vidhya

Data Scientist, Kaggle Expert (https://www.kaggle.com/itsmohammadshahid/code?scroll=true). Focusing on only one thing — To help people learn📚 🌱🎯️🏆