Python Forum
SQL select join operation in python(Select.. join)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL select join operation in python(Select.. join)
#1
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!!
Reply
#2
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]]) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  pymysql can't do SELECT * Pedroski55 3 286 Sep-11-2021, 10:18 PM
Last Post: Pedroski55
  Error using mariadb select query with form in python? shams 2 301 Jul-29-2021, 12:30 PM
Last Post: shams
  ''.join and start:stop:step notation for lists ringgeest11 1 548 Apr-08-2021, 04:29 PM
Last Post: BashBedlam
  Select data between DW felmback 2 554 Mar-21-2021, 01:49 PM
Last Post: ibreeden
  Subprocesses not opening File Select Dialog teut 2 560 Feb-22-2021, 08:07 PM
Last Post: teut
  os.path.join - errors out tester_V 4 717 Nov-29-2020, 08:57 AM
Last Post: DeaD_EyE
  The difference between os.path.join( and os.sep.join( Pedroski55 2 1,409 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Select a value from Dataframe mfkzolo 2 611 Nov-15-2020, 03:09 PM
Last Post: puneethr12
  os.path.join qmfoam 2 644 Nov-08-2020, 04:03 PM
Last Post: qmfoam
  Multi Select from SQL tehzeebahmed 6 864 Oct-19-2020, 12:35 AM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020