0

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 

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.