kaispace30098
kaispace30098

Reputation: 130

read_sql_query connection parameter con on multiple dataframes

I have three pandas dataframes and i would like to use SQL to join them together. i have a question on how i assign 'con' in pd.read_sql_query function.

import pandas as pd
df1 = pd.read_csv("C:/Users/name/sheet1.csv")
df2 = pd.read_csv("C:/Users/name/sheet2.csv")
df3 = pd.read_csv("C:/Users/name/sheet3.csv")



 sql=""" select t4.*,sum(t3.col1) from (select t1.col1,t2.col2 from df2 as t2 join df1 as t1 on t1.col1=t2.col2) as t4 join df3 as t3 on t4.col1 = t3.col3"""

df4=pd.read_sql_query(sql,  con=df1,df2,df3???) # here is the problem 

Upvotes: 0

Views: 449

Answers (1)

Wagner Gabriel
Wagner Gabriel

Reputation: 1

You can use the function merge() in pandas for together the dataframes. For use sql in a dataframe only possible just with connection some database.

Exemple of how does code using read_sql_table().

from sqlalchemy import create_engine  
engine = create_engine("sqlite:///database.db")  
with engine.connect() as conn, conn.begin():  
data = pd.read_sql_table("data", conn)

Exemple of merge(): https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.merge.html

Upvotes: 0

Related Questions