Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 16951

Inner Join Database Table with Excel file by query in python

$
0
0

I need to perform an inner join between a table in Amazon Redshift and an Excel table. The database table is quite large, and I only need to extract the IDs that are in Excel file, which contains a substantial list of IDs (approximately 30,000).

I attempted to include the Excel table directly into the execute function but encountered issues.How to correctly do that?

id_table=df['id']def query_():    query =f"""        SELECT distinct         t1.id, t1.value        FROM table1 t1        inner join  (SELECT id2 from %s ) as t2        on t1.id=t2.id2"""    return querydef get_dataframe2():    query = query_()    with psycopg2.connect(dbname=database_, host=server_, user=username_, password=password_, port=port_) as conn:        with conn.cursor() as cur:            cur.execute(query,(id_table,))            # cur.execute(query)            success = False            while not success:                try:                    data = cur.fetchall()                    success = True                except:                    cur.nextset()    cols = ['id','value']    return pd.DataFrame(data, columns=cols)

enter image description here


Viewing all articles
Browse latest Browse all 16951

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>