creating a pandas dataframe from a database query that uses bind variables

2024/11/17 19:29:23

I'm working with an Oracle database. I can do this much:

    import pandas as pdimport pandas.io.sql as psqlimport cx_Oracle as odbconn = odb.connect(_user +'/'+ _pass +'@'+ _dbenv)sqlStr = "SELECT * FROM customers"df = psql.frame_query(sqlStr, conn)

But I don't know how to handle bind variables, like so:

    sqlStr = """SELECT * FROM customers WHERE id BETWEEN :v1 AND :v2"""

I've tried these variations:

   params  = (1234, 5678)params2 = {"v1":1234, "v2":5678}df = psql.frame_query((sqlStr,params), conn)df = psql.frame_query((sqlStr,params2), conn)df = psql.frame_query(sqlStr,params, conn)df = psql.frame_query(sqlStr,params2, conn)

The following works:

   curs = conn.cursor()curs.execute(sqlStr, params)df = pd.DataFrame(curs.fetchall())df.columns = [rec[0] for rec in curs.description]

but this solution is just...inellegant. If I can, I'd like to do this without creating the cursor object. Is there a way to do the whole thing using just pandas?

Answer

Try using pandas.io.sql.read_sql_query. I used pandas version 0.20.1, I used it, it worked out:

import pandas as pd
import pandas.io.sql as psql
import cx_Oracle as odb
conn = odb.connect(_user +'/'+ _pass +'@'+ _dbenv)sqlStr = """SELECT * FROM customers WHERE id BETWEEN :v1 AND :v2
"""
pars = {"v1":1234, "v2":5678}
df = psql.frame_query(sqlStr, conn, params=pars)
https://en.xdnf.cn/q/71517.html

Related Q&A

Is there a docstring autocompletion tool for jupyter notebook?

I am looking for a tool/extension that helps you writing python docstrings in jupyter notebook. I normally use VS code where you have the autodocstring extension that automatically generates templates …

Long to wide data. Pandas

Im trying to take my dataframe from a long format in which I have a column with a categorical variable, into a wide format in which each category has its own price column. Currently, my data looks like…

How to wrap text in Django admin(set column width)

I have a model Itemclass Item(models.Model):id = models.IntegerField(primary_key=True)title = models.CharField(max_length=140, blank=True)description = models.TextField(blank=True)price = models.Decima…

Problems compiling mod_wsgi in virtualenv

Im trying to compile mod_wsgi (version 3.3), Python 2.6, on a CentOS server - but under virtualenv, with no success. Im getting the error:/usr/bin/ld:/home/python26/lib/libpython2.6.a(node.o):relocatio…

Python - Multiprocessing Error cannot start a process twice

I try to develop an algorithm using multiprocessing package in Python, i learn some tutorial from internet and try to develop an algorithm with this package. After looking around and try my hello world…

Printing unicode number of chars in a string (Python)

This should be simple, but I cant crack it. I have a string of Arabic symbols between u\u0600 - u\u06FF and u\uFB50 - u\uFEFF. For example غينيا واستمر العصبة ضرب قد. How do I pri…

Pandas report top-n in group and pivot

I am trying to summarise a dataframe by grouping along a single dimension d1 and reporting summary statistics for each element of d1. In particular I am interested in the top n (index and values) for …

virtualenv --no-site-packages is not working for me

virtualenv --no-site-packages v1cd v1\Scriptsactivate.batpython -c "import django" # - no problem hereWhy does it see the Django package??? It should give me an import error, right?

pandas: Group by splitting string value in all rows (a column) and aggregation function

If i have dataset like this:id person_name salary 0 [alexander, william, smith] 45000 1 [smith, robert, gates] 65000 2 [bob, alexander] …

Seaborn Title Position

The position of my graph title is terrible on this jointplot. Ive tried moving the loc = left, right, and center but it doesnt move from the position its in. Ive also tried something like ax.title.set_…