How can I populate a pandas DataFrame with the result of a Snowflake sql query?

后端 未结 2 2060
感动是毒
感动是毒 2021-02-13 16:10

Using the Python Connector I can query Snowflake:

import snowflake.connector

# Gets the version
ctx = snowflake.connector.connect(
    user=USER,
    password=P         


        
2条回答
  •  遥遥无期
    2021-02-13 16:27

    There is now a method .fetch_pandas.all() for this, no need for SQL Alchemy anymore.

    Note that you need to install snowflake.connector for pandas by doing this

    pip install snowflake-connector-python[pandas]
    

    Full documentation here

    import pandas as pd
    import snowflake.connector
    
    conn = snowflake.connector.connect(
                user="xxx",
                password="xxx",
                account="xxx",
                warehouse="xxx",
                database="MYDB",
                schema="MYSCHEMA"
                )
    
    cur = conn.cursor()
    
    # Execute a statement that will generate a result set.
    sql = "select * from MYTABLE limit 10"
    cur.execute(sql)
    # Fetch the result set from the cursor and deliver it as the Pandas DataFrame.
    df = cur.fetch_pandas_all()
    

提交回复
热议问题