0
$\begingroup$

I'm trying to use sqldf to do a left join on python. Here's my code:

query="select a.*, b.* from df1 as a left join df2 as b on a.name = b.name and a.year<=b.year"

df3=sqldf(query)

Even though df2 has the column 'name' (verified this), I get the following error:

PandaSQLException: (sqlite3.OperationalError) no such column: b.name
[SQL: select a.*, b.* from df1 as a left join df2 as b on a.name = b.name and a.year<=b.year]
(Background on this error at: https://sqlalche.me/e/14/e3q8)

Is there a format to be followed for the column names inside the SQL query? TIA.

$\endgroup$
2
  • $\begingroup$ The error code refers to an underlying connection error, not SQL format. Have you tried running the query directly on SQLite via the command line or gui? If it works there, you have hit something that is not in your code. Pandas also does joins, have you considered doing the join in Pandas itself? $\endgroup$
    – davmor
    Feb 4, 2023 at 15:58
  • $\begingroup$ I read online that this kind of join where you need to use ">=" instead of "=" may not be possible on pandas. I will try running the same on SQL. Thanks! $\endgroup$
    – Deep_S
    Feb 5, 2023 at 16:18

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.