I am adding in a DB the output of a model like this:
cursor = conn.cursor()
for i in range(len(df)):
UserId = df.loc[i, 'UserId']
Timestamp = df.loc[i, 'Timestamp']
ChurnPropensity = df.loc[i, 'ChurnPropensity']
sql = "INSERT INTO DB_Name (UserId, Timestamp, ChurnPropensity) VALUES ({},'{}',{});".format(UserId, Timestamp, ChurnPropensity)
cursor.execute(sql)
conn.commit()
However, it takes a long time due to the for loop. How would you improve computation time?
Salut,
pd.DataFrame.to_sql?pd.DataFrame.to_sqlworking, and time was reduced by 90% :)to_sqlbecause of the I/O. You can try building raw queries and then using sqlalchemy to execute them as well. But great it helped!