Skip to main content

Today we are going to see how to use our Snowflake Data with Pandas in Python. In a previous blog we already saw how to connect Snowflake with Python, so let’s reuse that example.

The data that we are going to load into Pandas is the following table that we created in the previous blog and is stored in our Snowflake account.

IDNAMEEMAILNUM
1Seana Maunderssmaunders0@samsung.com555-983-6281
2Leanna Entreslentres1@miibeian.gov.cn956-437-2011
3Cherin Geydoncgeydon2@stumbleupon.com692-616-2135
4Klarrisa Thurlbeckkthurlbeck3@ocn.ne.jp767-554-5346
5Dwayne Hurlingdhurling4@cbc.ca418-885-5011
6Rochette Ballhamrballham5@rambler.ru325-152-2767
7Brenna Fruishbfruish6@google.com.hk766-239-1379
8Al Deareadeare7@networksolutions.com845-560-8781
9Felicdad McClarencefmcclarence8@salon.com129-698-9707
10Kiley Readheadkreadhead9@instagram.com107-619-2098
Mock data that we are going to use

Initially, we have to install the version of the Snowflake connector that is capable of working with pandas

pip install "snowflake-connector-python[pandas]"

Then, in pyhton we can create the Snowflake connection (see the previous blog) to query the database.

import snowflake.connector
 
con = snowflake.connector.connect(
    user='python_user',
    password='python_password',
    account='<account_identifier>',
    warehouse='compute_wh',
    database='python_database',
    schema='public'
)

Now we must create a cursor object, which is basically used to execute the queries and store their information and results.

cur = con.cursor()
cur.execute('SELECT * FROM cool_table;')

And finally, we can use two methods in order to fetch the retrieved data from the cursor object. Either we can use cur.fetch_pandas_all(), so

df = cur.fetch_pandas_all()

which returns all the results in a single dataframe, or we can use cur.fetch_pandas_batches(), which returns an iterator that produces subsets of all the row results of the query

for df in cur.fetch_pandas_batches():
    do_dataframe_stuff(df)

The result of the two is, as expected, a dataframe (or a set of dataframes) with the information that we could see in the table above. Using these cursor methods we obtain the dataframe with the column names as determined by the SQL query. So, overall, pretty useful for our Data Analysis python scripts.

And for those that were expecting a very long and intricate explanation on how we could do it: Yes! Using your Snowflake Data in Pandas really is this simple!

Complete code

Below you can see the complete code used for this blog.

import snowflake.connector

# Create the Snowflake-Python connection
con = snowflake.connector.connect(
    user='python_user',
    password='python_password',
    account='<account_identifier>',  # Change this to your own account
    warehouse='compute_wh',
    database='python_database',
    schema='public'
)

# Create the cursor and execute the query
cur = con.cursor()
cur.execute('SELECT * FROM cool_table;')

# Gather all the results in a single dataframe
df = cur.fetch_pandas_all()
print(df)

# Gather the results loaded into batches of smaller size
for df in cur.fetch_pandas_batches():
    print(df)

Auteur

  • Eloi Sanchez

    Eloi studied Chemistry at the University of Barcelona, but rapidly leaned on the mathematical and physical part of the field, called Physical Chemistry. Later on, he studied a Master's in Atomistic and Multiscale Computational Modelling, were he focused on Computational Quantum Physics. During the last years, he is been mostly interested in the Data field and is currently studying a Master's in Data Science. He likes to spend its time in nature and he is an animal lover. There are no specific hobbies that define him because he is usually always trying new things, although bouldering, playing chess and videogames and hiking are recurrent in his daily life. When looking for new opportunities in the Data job market, he found the Nimbus Intelligence Academy. It is the perfect step for him in order to formalize the switch from academy to the private sector, and it is an incredible opportunity for professional and personal growth.

Eloi Sanchez

Eloi studied Chemistry at the University of Barcelona, but rapidly leaned on the mathematical and physical part of the field, called Physical Chemistry. Later on, he studied a Master's in Atomistic and Multiscale Computational Modelling, were he focused on Computational Quantum Physics. During the last years, he is been mostly interested in the Data field and is currently studying a Master's in Data Science. He likes to spend its time in nature and he is an animal lover. There are no specific hobbies that define him because he is usually always trying new things, although bouldering, playing chess and videogames and hiking are recurrent in his daily life. When looking for new opportunities in the Data job market, he found the Nimbus Intelligence Academy. It is the perfect step for him in order to formalize the switch from academy to the private sector, and it is an incredible opportunity for professional and personal growth.