简体   繁体   中英

How to perform SQL Join with 2 conditions in Python?

I have a SQL Query which joins two tables:

SQL => select * from TAL left join TNA on TNA.TaskId = 123 and TAL.Id = TNAA.Id

In Python, I've read the tables from Excel and now I want to write the same logic. Is there any way through this can be achieved?

Note: pandasql library works and does the same job. However, I am looking for a solution which can be done without writing code in SQL format.

Using pandas , you could do:

TAL[TAL.Id == TNAA.Id].merge(TNA[TNA.TaskId == 123],
    left_on='Id',
    right_on='TaskId',
    how='left')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM