'Python Pandas data frame to_csv to insert data into a new column
I'm using Snowflake connector to connect to a Snowflake database using python, I can connect, run queries, retrieve the results data and export it into a csv file (as a new row every time), I'd like to add the data into a new column in the csv file each time I run a new query.
Example : first query in the below code will populate cell A1 in my csv, so I want next result to be populated in G1, and the query after to be from M1..etc. (screenshot below)
Here is my code:
import snowflake.connector
import pandas as pd
import csv
with open('queries.csv') as f:
lines = f.readlines()
user_email = "[email protected]"
query = lines[0]
conn = snowflake.connector.connect(user=user_email,
account='xxxxxxxx',
authenticator="externalbrowser",
database='xxxxx',
schema='xxxxxx',
warehouse='xxxxx',
autocommit=True)
query_output = conn.cursor().execute(query)
old_week = query_output.fetchall()
for old_week_row in old_week:
val_old_week = old_week_row[0]
with open('export.csv','a') as fd:
fd.write(str(val_old_week))
query = lines[2]
query_output = conn.cursor().execute(query)
query_output.fetch_pandas_all().to_csv("export.csv",index=False,header=False,mode="a")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

