Python Models
Build advanced data transformations, ML predictions, and statistical analysis with Python templates.
Last reviewed September 15, 2026
Python models run custom Python code against your BigQuery data for advanced logic. Examples are customer segmentation, predictive scoring, and recommendation engines. Start from a pre-built template or write your own.
Creating a Python transformation
- Open Studio.
- Click New Data Model.
- Select Python.
- In Input data, select the connected sources that the model reads.
- Set the output table name and the Write mode.
- Customize the Python code and parameters.
- Save the model.
Template Categories
| Category | Templates | Use Case |
|---|---|---|
| Recommendations | Product affinity, cross-sell models | Suggest products based on purchase history |
| Segmentation | RFM analysis, behavioral clustering | Group customers by behavior or value |
| Prediction | Churn prediction, LTV forecasting | Predict future customer behavior |
| Analytics | Customer Acquisition Cost, Cohort Retention Analysis, Forecasting | Measure acquisition cost, retention and trends |
Input Tables
Map one or more BigQuery tables as inputs to your model. Each input table is available in your code as a pandas DataFrame, with the alias of the input as the variable name.
Output Configuration
| Setting | Description |
|---|---|
| Output Table | Name of the BigQuery table to write results to |
| Write Mode | replace (overwrite table each run) or append (add new rows) |
Writing Python Code
Your code runs in a secure environment with these libraries:
| Library | Version | Purpose |
|---|---|---|
| pandas | 2.x | Data manipulation |
| numpy | 1.x | Numerical computing |
| datetime, math, json, re, collections, itertools | Python standard library | Dates, math, JSON, regular expressions and iteration helpers |
Code Structure
Your code receives the mapped input tables as DataFrames. It must set the output or result variable to a DataFrame:
import pandas as pd
# RFM segmentation example
rfm = orders_df.groupby('customer_id').agg({
'created_at': 'max', # Recency
'id': 'count', # Frequency
'total_price': 'sum' # Monetary
}).reset_index()
rfm.columns = ['customer_id', 'last_order', 'frequency', 'monetary']
output = rfmNote: Vendo disables external network access for security. All data must come from the configured input tables.
Scheduling and Triggers
Python models can run in these ways:
| Trigger | Description |
|---|---|
| Manual | Run on demand from the model detail page |
| Source trigger | Run automatically when a source app finishes syncing |
| Model trigger | Run after an upstream model completes |
Chaining Models
Set a source model trigger to make a pipeline of models. For example:
- Source sync imports Shopify orders
- SQL model computes daily revenue (triggered by source sync)
- Python model runs RFM segmentation (triggered by the SQL model)
Vendo detects circular dependencies and prevents chains longer than 10 models.
Best Practices
- Keep models focused: one transformation for each model.
- Use descriptive output table names: for example
customer_segmentsorproduct_recommendations. - Handle edge cases: check for empty DataFrames and null values.
- Start from templates: customize a pre-built template instead of writing from scratch.
- Use
replacemode for idempotent outputs, andappendfor time-series accumulation.
Related
- Data Studio
- SQL Models: for query-based transformations
- Audiences: for user lists that you sync to ad platforms
- Agent → Building Models: create Python models with AI assistance