Home > database >  Multiple WHERE conditions in Pandas read_sql
Multiple WHERE conditions in Pandas read_sql

Time:01-03

I've got my data put into an SQLite3 database, and now I'm trying to work on a little script to access data I want for given dates. I got the SELECT statement to work with the date ranges, but I can't seem to add another condition to fine tune the search.

db columns id, date, driverid, drivername, pickupStop, pickupPkg, delStop, delPkg

What I've got so far:

import pandas as pd
import sqlite3


sql_data = 'driverperformance.sqlite'
conn = sqlite3.connect(sql_data)
cur = conn.cursor()

date_start = "2021-12-04"
date_end = "2021-12-10"
df = pd.read_sql_query("SELECT DISTINCT drivername FROM DriverPerf WHERE date BETWEEN :dstart and :dend", params={"dstart": date_start, "dend": date_end}, con=conn)
drivers = df.values.tolist()

for d in drivers:
    driverDF = pd.read_sql_query("SELECT * FROM DriverPerf WHERE drivername = :driver AND date BETWEEN :dstart and :dend", params={"driver": d, "dstart": date_start, "dend": date_end}, con=conn)

I've tried a few different versions of the "WHERE drivername" part but it always seems to fail.

Thanks!

CodePudding user response:

If I'm not mistaken, drivers will be a list of lists. Have you tried

.... params={"driver": d[0] ....
  • Related