Open In App

Working with database using Pandas

Performing various operations on data saved in SQL might lead to performing very complex queries that are not easy to write. So to make this task easier it is often useful to do the job using pandas which are specially built for data preprocessing and is more simple and user-friendly than SQL.

There might be cases when sometimes the data is stored in SQL and we want to fetch that data from SQL in python and then perform operations using pandas. So let’s see how we can interact with SQL databases using pandas.



This is the database we are going to work with
diabetes_data

Note: Assuming that the data is stored in sqlite3



Reading the data




# import the libraries
import sqlite3
import pandas as pd
  
# create a connection
con = sqlite3.connect('Diabetes.db')
  
# read data from SQL to pandas dataframe.
data = pd.read_sql_query('Select * from Diabetes;', con)
  
# show top 5 rows
data.head()

Output

Basic operation


Article Tags :