📖 Scenario: You are working with Snowflake's Snowpark for Python to process data inside the cloud database. You want to create a simple Snowpark session, load some sample data into a DataFrame, and perform a basic transformation.
🎯 Goal: Build a Snowpark Python script that creates a session, loads a small dataset into a DataFrame, filters the data, and shows the result.
📋 What You'll Learn
Create a Snowpark session with exact parameters
Create a DataFrame from a list of tuples with specific data
Filter the DataFrame using a condition on a column
Show the filtered DataFrame content
💡 Why This Matters
🌍 Real World
Snowpark for Python lets you run Python code close to your data inside Snowflake, reducing data movement and improving performance.
💼 Career
Data engineers and analysts use Snowpark to build scalable data pipelines and transformations directly in Snowflake using Python.
Progress0 / 4 steps
1
Create a Snowpark session
Write code to create a Snowpark session called session using Session.builder.configs() with the exact dictionary {'account': 'xy12345', 'user': 'user1', 'password': 'pass123', 'role': 'SYSADMIN', 'warehouse': 'COMPUTE_WH', 'database': 'TEST_DB', 'schema': 'PUBLIC'} and then call .create().
Snowflake
Hint
Use Session.builder.configs() with the exact dictionary and then call .create() to make the session.
2
Create a DataFrame with sample data
Create a variable called data that is a list of tuples with these exact entries: (1, 'Apple'), (2, 'Banana'), (3, 'Cherry'). Then create a Snowpark DataFrame called df from data using session.create_dataframe(data, schema=['id', 'fruit']).
Snowflake
Hint
Make a list called data with the exact tuples. Then use session.create_dataframe() with data and the schema list ['id', 'fruit'].
3
Filter the DataFrame for fruits with id greater than 1
Create a new DataFrame called filtered_df by filtering df where the id column is greater than 1 using df.filter(df['id'] > 1).
Snowflake
Hint
Use df.filter(df['id'] > 1) to create filtered_df.
4
Show the filtered DataFrame content
Call filtered_df.show() to display the filtered data.
Snowflake
Hint
Call filtered_df.show() to display the filtered rows.
Practice
(1/5)
1. What is the main purpose of Snowpark for Python in Snowflake?
easy
A. To run Python code directly inside Snowflake for data processing
B. To create visual dashboards from Snowflake data
C. To export Snowflake data to external Python scripts only
D. To replace SQL queries with Java code
Solution
Step 1: Understand Snowpark for Python's role
Snowpark for Python allows running Python code inside Snowflake, enabling fast data processing close to the data.
Step 2: Compare options to the main purpose
Creating visual dashboards, exporting Snowflake data to external Python scripts only, and replacing SQL queries with Java code describe other unrelated tasks or languages, while running Python code directly inside Snowflake for data processing correctly states the main purpose.
Final Answer:
To run Python code directly inside Snowflake for data processing -> Option A
Quick Check:
Snowpark runs Python inside Snowflake = A [OK]
Hint: Snowpark runs Python code inside Snowflake, not outside [OK]
Common Mistakes:
Confusing Snowpark with visualization tools
Thinking Snowpark exports data only
Assuming Snowpark uses Java
2. Which of the following is the correct way to create a Snowpark session in Python?
easy
A. session = SnowflakeSession.connect(connection_parameters)
B. session = snowpark.Session.create(connection_parameters)
C. session = snowflake.snowpark.Session.builder.configs(connection_parameters).create()
D. session = snowflake.connect.snowpark(connection_parameters)
Solution
Step 1: Recall Snowpark session creation syntax
The correct method uses the builder pattern: snowflake.snowpark.Session.builder.configs(...).create()
Step 2: Check each option's syntax
Only session = snowflake.snowpark.Session.builder.configs(connection_parameters).create() matches the official Snowpark Python API syntax; others are incorrect or invalid.
Final Answer:
session = snowflake.snowpark.Session.builder.configs(connection_parameters).create() -> Option C
Quick Check:
Session builder pattern = B [OK]
Hint: Use Session.builder.configs(...).create() to start Snowpark session [OK]
Common Mistakes:
Using incorrect method names like connect()
Wrong module or class names
Missing builder pattern
3. Given the following Snowpark Python code, what will df.show() display?
The col function must be imported from snowflake.snowpark.functions to be used.
Step 2: Verify other parts
Table name SALES can be valid, filter condition < 1000 is valid, and show() exists on DataFrame.
Final Answer:
The col function is not imported or defined -> Option D
Quick Check:
Missing import for col() causes error = C [OK]
Hint: Always import col() from snowflake.snowpark.functions before filtering [OK]
Common Mistakes:
Forgetting to import col()
Assuming show() is missing
Confusing filter operators
5. You want to create a new DataFrame with employee names in uppercase from the EMPLOYEES table using Snowpark for Python. Which code snippet correctly does this?
hard
A. df = session.table('EMPLOYEES').select(upper('NAME'))
B. df = session.table('EMPLOYEES').select(upper(col('NAME')).alias('NAME_UPPER'))
C. df = session.table('EMPLOYEES').select('NAME').map(str.upper)
D. df = session.table('EMPLOYEES').select(col('NAME').upper())
Solution
Step 1: Use upper() function with col()
To convert a column to uppercase, use upper(col('NAME')) and alias it for clarity.
Step 2: Check each option's correctness
df = session.table('EMPLOYEES').select(upper(col('NAME')).alias('NAME_UPPER')) uses correct syntax with upper() and col(). df = session.table('EMPLOYEES').select(col('NAME').upper()) tries to call upper() on a column object, which is invalid. df = session.table('EMPLOYEES').select('NAME').map(str.upper) uses map on DataFrame, which is not supported. df = session.table('EMPLOYEES').select(upper('NAME')) passes string directly to upper(), which is incorrect.
Final Answer:
df = session.table('EMPLOYEES').select(upper(col('NAME')).alias('NAME_UPPER')) -> Option B
Quick Check:
Use upper(col('NAME')) with alias = D [OK]
Hint: Use upper(col('COLUMN')) and alias() to transform columns [OK]
Common Mistakes:
Calling upper() as a method on column
Using map() on DataFrame
Passing column name as string to upper()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.