Read from SQL databases - Python Pandas Module

Read from SQL databases - Python Pandas Module

Instructor-svg Al-Mamun Sarkar
Apr 01 , 2020

Read from SQL databases - Python Pandas Module. The following code shows how to read data from SQL database for working with the Python Pandas module.

 

In [1]:

import pandas as pd
import mysql.connector

 

Here we use the sqlalchemy library to grab data from a sql database

 

In [2]:

conn = mysql.connector.connect(user='root', password='root', database='pandas')
cursor = conn.cursor()
cursor.execute("SELECT * FROM test")
data = cursor.fetchall()

 

In [3]:

my_data_frame = pd.DataFrame(data=list(data))
my_data_frame

Out[3]:

  0 1 2 3
0 1 Hello 12 2018-02-10 14:20:11
1 2 World 324 2018-02-10 14:20:11
2 3 Hello 12 2018-02-10 14:20:20
3 4 World 324 2018-02-10 14:20:20
4 5 Hello 12 2018-02-10 14:20:20
5 6 World 324 2018-02-10 14:20:20
6 7 Hello 12 2018-02-10 14:20:20
7 8 World 324 2018-02-10 14:20:20
8 9 Hello 12 2018-02-10 14:20:20
9 10 World 324 2018-02-10 14:20:20
10 11 Hello 12 2018-02-10 14:20:20
11 12 World 324 2018-02-10 14:20:20
12 13 Hello 12 2018-02-10 14:20:20
13 14 World 324 2018-02-10 14:20:20
  • Share On:
  • fb
  • twitter
  • pinterest
  • instagram