Skip to content

Instantly share code, notes, and snippets.

@cjbj
Last active August 18, 2025 23:53
Show Gist options
  • Select an option

  • Save cjbj/1e3673508dedce60cefb4d57b4e67939 to your computer and use it in GitHub Desktop.

Select an option

Save cjbj/1e3673508dedce60cefb4d57b4e67939 to your computer and use it in GitHub Desktop.
Querying a DataFrame using python-oracledb from the video "Python DataFrames with Oracle Database for Analysis and AI" https://youtu.be/0BJNlbh71LY For other examples, see https://github.com/oracle/python-oracledb/tree/main/samples
# pandas-new.py
#
# For other python-oracledb DataFrame examples, see https://github.com/oracle/python-oracledb/tree/main/samples
import os
import pyarrow
import oracledb
un = os.environ.get('ORACLE_USERNAME')
pw = os.environ.get('ORACLE_PASSWORD')
cs = os.environ.get('ORACLE_DSN')
connection = oracledb.connect(user=un, password=pw, dsn=cs)
#-------------------------------------------------------------------------------
sql = """select employee_id, department_id, salary
from employees
where department_id < :id
order by department_id"""
# The new, fast way to get a DataFrame
odf = connection.fetch_df_all(
statement=sql,
parameters=[80],
arraysize=1000
)
# Use PyArrow to do zero-copy conversion to our chosen package's DataFrame
df = pyarrow.table(odf).to_pandas()
#-------------------------------------------------------------------------------
print()
# Sum of salaries in each department
deptsum = df.groupby('DEPARTMENT_ID').sum()
print(deptsum)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment