create_dataframe is a Snowpark session method for constructing a DataFrame from local row data and an optional schema. A StructType schema declares each column through StructField objects, including names and data types such as DateType and BooleanType. section 6.5
Listing 6.9 Constructing a data frame and defining the data structure
# create a data frame from the holiday_flags list of lists
# and define the schema as two columns:
# - column named "day" with data type DateType
# - column named "holiday_flg" with data type BooleanType
df = my_session.create_dataframe(
holiday_flags,
schema = StructType(
[StructField("day", DateType()),
StructField("holiday_flg", BooleanType())])
)
Construct a holiday dimension from holiday_flags, defining day as a date and holiday_flg as a Boolean column. section 6.5
Use the method within a Snowpark pipeline as follows:
- Create a Snowflake connection and call create_dataframe with the local list of lists and its StructType schema. section 6.5
- Remember that DataFrame operations are lazy; collect(), count(), show(), or save_as_table() triggers execution. section 6.5
- Persist results with write.mode("overwrite").save_as_table() during iterative development, or append records when loading additional data. section 6.5 section 7.4.3
Convert CSV output into a table
Split CSV text into rows, remove the header, pass the resulting list of lists and a five-column schema to create_dataframe, then append the DataFrame to COLLECTED_ORDERS_FROM_EMAIL. section 7.4.3
orders_df = session.create_dataframe(
[x.split(',') for x in csv_output.split("\n")][1:], schema)
Inspect the concise conversion from parsed CSV rows to an orders DataFrame using a predefined schema. section 7.4.3
