To export a query from Snowflake to a Pandas DataFrame in Python, you can use the `snowflake-connector-python` package. This allows you to fetch data directly into DataFrame format with just a few lines of code. Begin by installing the snowflake-connector with `pip install snowflake-connector-python`. Then, structure your script to connect to your Snowflake account using your credentials, execute the query, and fetch the results into a DataFrame:
import snowflake.connector
import pandas as pd
Establishing the Snowflake connection
conn = snowflake.connector.connect(
user='<your_username>',
password='<your_password>',
account='<your_account_url>',
warehouse='<your_warehouse>',
database='<your_database>',
schema='<your_schema>'
)
Execute a query and fetch the result into a pandas DataFrame
query = "SELECT * FROM YOUR_TABLE_NAME;"
cursor = conn.cursor()
cursor.execute(query)
df = pd.DataFrame(cursor.fetchall(), columns=[x[0] for x in cursor.description])
Always close the cursor and connection when done
cursor.close()
conn.close()
Replace the placeholder values with your actual Snowflake credentials and modify the query as per your requirements. After running this script, `df` will contain the result of the Snowflake query as a Pandas DataFrame.