Copyright © 2016, Oracle and/or its affiliates. All rights reserved. |
Python Development Example
Read data from Oracle Database into Pandas DataFrame
Johan Louwers – Chief Customer Architect @ Oracle
Version : Feb 2019
@johanlouwers
Johanlouwers.blogspot.com
Oracle Confidential – Internal/Restricted/Highly Restricted
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. |
Safe Harbor Statement
The following is intended to outline our general product direction. It is intended for
information purposes only, and may not be incorporated into any contract. It is not a
commitment to deliver any material, code, or functionality, and should not be relied upon
in making purchasing decisions. The development, release, and timing of any features or
functionality described for Oracle’s products remains at the sole discretion of Oracle.
2
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. |
Python Development
with pandas and CX_Oracle
Slide-deck Intention :
• This presentation is intended to provide
a quick introduction example on how to
load data from an Oracle Database into
pandas.
• This example is a part of a wider
workshop deck and shared as a stand-
alone example for ease of sharing.
• The code should only be used as an
educational example and is not
intended to be included in any real-
world application code.
3
Contact details :
Johan Louwers - Chief Customer Architect
@johanlouwers
Johan.louwers@oracle.com
github.com/louwersj
Johanlouwers.blogspot.com
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | 4
Python Development
Pandas – read data from database into DataFrame
A B C D
0 A0 B0 C0 D0
1 A1 B1 C1 D1
2 A2 B2 C2 D2
pd.read_sql
What do we want to achieve?
1) Read data from an Oracle database into a DataFrame based upon a SQL query.
2) We use cx_Oracle in combination with Oracle Database 18c
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | 5
import pandas as pd
import cx_Oracle
orclUserName = 'pandas_test'
orclPassword = 'pandas_test'
orclHost = '192.168.33.10'
orclPort = '1521'
orclServiceName = 'pdb1'
dsn = cx_Oracle.makedsn(host=orclHost,
port=orclPort,
sid=None,
service_name=orclServiceName
)
connection = cx_Oracle.connect(user=orclUserName,
password=orclPassword,
dsn=dsn,
encoding="UTF-8",
nencoding="UTF-8"
)
query = """SELECT * FROM emp"""
df_ora = pd.read_sql(query, con=connection)
print (df_ora.head())
Import
cx_Oracle
Provide needed
details
Define the Data
Source Name
Define the
connection
Define the
query
Execute the
read_sql()
Python Development
Pandas – read data from database into DataFrame
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | 6
Show me the output
1) You can verify the output of df_ora.head() with the content of your database EMP table
Python Development
Pandas – read data from database into DataFrame
Copyright © 2015 Oracle and/or its affiliates. All rights reserved. | Oracle Confidential – Internal/Restricted/Highly Restricted 7
import data from Oracle Database into Python Pandas Dataframe

import data from Oracle Database into Python Pandas Dataframe

  • 1.
    Copyright © 2016,Oracle and/or its affiliates. All rights reserved. | Python Development Example Read data from Oracle Database into Pandas DataFrame Johan Louwers – Chief Customer Architect @ Oracle Version : Feb 2019 @johanlouwers Johanlouwers.blogspot.com Oracle Confidential – Internal/Restricted/Highly Restricted
  • 2.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle’s products remains at the sole discretion of Oracle. 2
  • 3.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | Python Development with pandas and CX_Oracle Slide-deck Intention : • This presentation is intended to provide a quick introduction example on how to load data from an Oracle Database into pandas. • This example is a part of a wider workshop deck and shared as a stand- alone example for ease of sharing. • The code should only be used as an educational example and is not intended to be included in any real- world application code. 3 Contact details : Johan Louwers - Chief Customer Architect @johanlouwers Johan.louwers@oracle.com github.com/louwersj Johanlouwers.blogspot.com
  • 4.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | 4 Python Development Pandas – read data from database into DataFrame A B C D 0 A0 B0 C0 D0 1 A1 B1 C1 D1 2 A2 B2 C2 D2 pd.read_sql What do we want to achieve? 1) Read data from an Oracle database into a DataFrame based upon a SQL query. 2) We use cx_Oracle in combination with Oracle Database 18c
  • 5.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | 5 import pandas as pd import cx_Oracle orclUserName = 'pandas_test' orclPassword = 'pandas_test' orclHost = '192.168.33.10' orclPort = '1521' orclServiceName = 'pdb1' dsn = cx_Oracle.makedsn(host=orclHost, port=orclPort, sid=None, service_name=orclServiceName ) connection = cx_Oracle.connect(user=orclUserName, password=orclPassword, dsn=dsn, encoding="UTF-8", nencoding="UTF-8" ) query = """SELECT * FROM emp""" df_ora = pd.read_sql(query, con=connection) print (df_ora.head()) Import cx_Oracle Provide needed details Define the Data Source Name Define the connection Define the query Execute the read_sql() Python Development Pandas – read data from database into DataFrame
  • 6.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | 6 Show me the output 1) You can verify the output of df_ora.head() with the content of your database EMP table Python Development Pandas – read data from database into DataFrame
  • 7.
    Copyright © 2015Oracle and/or its affiliates. All rights reserved. | Oracle Confidential – Internal/Restricted/Highly Restricted 7