Python Forum
SQL select join operation in python(Select.. join) - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: SQL select join operation in python(Select.. join) (/thread-16123.html)



SQL select join operation in python(Select.. join) - pradeepkumarbe - Feb-14-2019

I have data set called Account_universe
ACC_ID ZIP_CODE
001AAD 638400
002ADF 638402
003AAS 638902
004AGF 123480
005AQA 987650

Next data set Alt_Account_univ
ACC_ID ALT_ID SOURCE
001AAD 10000 confd
002ADF 10001 Non confd
003AAS 10002 Non confd
004AGF 10003 Non confd
005AQA 10004 confd


In sql I will do like

Select a.ACCT_ID,b.ALT_ID,a.ZIP_CODE
from Account_universe a
left join Alt_Account_univ b on b.acct_id = a.acct_id


In pandas I will do like

df_a = Account_universe
df_b= Alt_Account_univ
df1 =df_a.merge(df_b[['ACCT_ID','ALT_ID']],on='ACCT_ID',how='left')
What I have tried in python without(pandas)

set(df_a) & set(df_b)
Here how to join on specific columns and I have to write only a.ACCT_ID,b.ALT_ID,a.ZIP_CODE these columns in another list. I am aware this set will act like A intersect B any other possible ways to do this operation?.

Thanks in Advance!!


RE: SQL select join operation in python(Select.. join) - woooee - Feb-14-2019

If I understand you it would be something along the lines of
## using you pseudo-code
Select ACCT_ID, ZIP_CODE from a 
recs_a=cur_a.fetchall()
select ALT_ID from b 
recs_b=cur_b.fetchall()

combined_list=[]
for ctr, row in enumerate(recs_a):  ## assumes a and b are the same length
    combined_list.append([row[0], recs_b[ctr], row[1]])