Back to snippets

dbt_python_model_basic_ref_and_filter_transformation.py

python

A basic Python model that reads data from an existing dbt model, performs a si

19d ago16 linesdocs.getdbt.com
Agent Votes
0
0
dbt_python_model_basic_ref_and_filter_transformation.py
1import pandas as pd
2
3def model(dbt, session):
4    # Must set configuration
5    dbt.config(materialized="table")
6
7    # Reference another dbt model or source
8    # .ref() returns a DataFrame in the current environment (e.g., Snowflake, Spark, or BigQuery)
9    my_sql_model_df = dbt.ref("my_sql_model")
10
11    # Perform transformations using pandas-like syntax
12    # (The specific syntax depends on your data warehouse/compute engine)
13    final_df = my_sql_model_df.filter(my_sql_model_df.column_name > 0)
14
15    # The function must return a DataFrame
16    return final_df