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"


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.

  • $\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 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 at 16:18


Your Answer

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

Browse other questions tagged or ask your own question.