Expression Columns#

Expression columns compute values using Jinja2 expressions that can reference other columns. This powerful feature allows you to create derived columns, combine data from multiple sources, and implement complex business logic.

Before You Start#

Before getting started, ensure you have the Data Designer client and configuration builder set up:

import os
from nemo_microservices import NeMoMicroservices
from nemo_microservices.beta.data_designer import DataDesignerClient, DataDesignerConfigBuilder
from nemo_microservices.beta.data_designer.config import columns as C
from nemo_microservices.beta.data_designer.config import params as P

data_designer_client = DataDesignerClient(
    client=NeMoMicroservices(base_url=os.environ["NEMO_MICROSERVICES_BASE_URL"])
)

config_builder = DataDesignerConfigBuilder(model_configs="path/to/your/model_configs.yaml")

String Concatenation#

Combine text from multiple columns:

config_builder.add_column(
    name="full_address",
    type="expression",
    expr="{{ street_address }}, {{ city }}, {{ state }} {{ zip_code }}"
)
config_builder.add_column(
    C.ExpressionColumn(
        name="full_address",
        expr="{{ street_address }}, {{ city }}, {{ state }} {{ zip_code }}"
    )
)

Mathematical Operations#

Perform calculations with numeric columns:

# Calculate area from length and width
config_builder.add_column(
    name="area",
    type="expression",
    expr="{{ length * width }}"
)

# Calculate percentage
config_builder.add_column(
    name="discount_percentage",
    type="expression",
    expr="{{ (original_price - sale_price) / original_price * 100 }}"
)
# Calculate area from length and width
config_builder.add_column(
    C.ExpressionColumn(
        name="area",
        expr="{{ length * width }}"
    )
)

# Calculate percentage
config_builder.add_column(
    C.ExpressionColumn(
        name="discount_percentage",
        expr="{{ (original_price - sale_price) / original_price * 100 }}"
    )
)

Conditional Logic#

Use Jinja2 conditional statements:

config_builder.add_column(
    name="shipping_cost",
    type="expression",
    expr="{% if order_total > 50 %}0{% else %}5.99{% endif %}"
)

# More complex conditional
config_builder.add_column(
    name="customer_tier",
    type="expression",
    expr="""
    {%- if total_purchases > 1000 -%}
    Premium
    {%- elif total_purchases > 500 -%}
    Gold
    {%- elif total_purchases > 100 -%}
    Silver
    {%- else -%}
    Bronze
    {%- endif -%}
    """
)
config_builder.add_column(
    C.ExpressionColumn(
        name="shipping_cost",
        expr="{% if order_total > 50 %}0{% else %}5.99{% endif %}"
    )
)

# More complex conditional
config_builder.add_column(
    C.ExpressionColumn(
        name="customer_tier",
        expr="""
        {%- if total_purchases > 1000 -%}
        Premium
        {%- elif total_purchases > 500 -%}
        Gold
        {%- elif total_purchases > 100 -%}
        Silver
        {%- else -%}
        Bronze
        {%- endif -%}
        """
    )
)

Person Attribute Expressions#

When using person samplers, you can reference person attributes in expressions:

Setup person sampler:

config_builder.with_person_samplers({"customer": {"locale": "en_US"}})

Reference person attributes:

config_builder.add_column(
    name="customer_full_name",
    type="expression",
    expr="{{ customer.first_name }} {{ customer.last_name }}"
)

config_builder.add_column(
    name="welcome_message",
    type="expression",
    expr="Welcome {{ customer.first_name }}! You're from {{ customer.city }}, {{ customer.state }}."
)
config_builder.add_column(
    C.ExpressionColumn(
        name="customer_full_name",
        expr="{{ customer.first_name }} {{ customer.last_name }}"
    )
)

config_builder.add_column(
    C.ExpressionColumn(
        name="welcome_message",
        expr="Welcome {{ customer.first_name }}! You're from {{ customer.city }}, {{ customer.state }}."
    )
)

Data Type Conversion#

Expression columns can specify output data types:

config_builder.add_column(
    name="total_items",
    type="expression",
    expr="{{ quantity_a + quantity_b }}",
    convert_to="int"  # Convert result to integer
)

config_builder.add_column(
    name="is_eligible",
    type="expression",
    expr="{{ age >= 18 }}",
    convert_to="bool"  # Convert result to boolean
)
config_builder.add_column(
    C.ExpressionColumn(
        name="total_items",
        expr="{{ quantity_a + quantity_b }}",
        convert_to="int"
    )
)

config_builder.add_column(
    C.ExpressionColumn(
        name="is_eligible",
        expr="{{ age >= 18 }}",
        convert_to="bool"
    )
)

For more detailed information about Jinja2 templating, see the Using Jinja Templates guide.