Skip to main content
For example, to multiply column A by two and add column Col B, you would simply write
calculate(ds[["A", "Col B"]], {
    "formula": "2 * A - `Col B`"
}) -> (ds.result)
For more details regarding valids operators etc. see the Pandas eval() documentation, more specifically the supported syntax, and eval() applied to DataFrames. Note that assignments in the formula are not supported, since the result must always be a new column, i.e. the following kind of formula should be avoided: "c = a + b". The correct way to return the result as a column would simply be "a + b". If the name of an input column contains spaces, such as in the example above, it should be quoted in single backticks.

Usage

The following example shows how the step can be used in a recipe.

Examples

  • Example 1
  • Signature
Assuming a dataset ds containing the numeric columns num_a, num_b and num_c, and a constant a with value 1.3, the following formula transforms each column numerically before adding and multiplying them together, all in one step:
calculate(ds, {
    "formula": "log(num_a) + num_b**3 * (num_c + @a)",
    "constants": {"a": 1.3}
}) -> (ds.result)

Inputs & Outputs

The following are the inputs expected by the step and the outputs it produces. These are generally columns (ds.first_name), datasets (ds or ds[["first_name", "last_name"]]) or models (referenced by name e.g. "churn-clf").
ds
dataset
required
A dataset with columns to be used in the evaluation of the formula. Note, all columns mentioned in the formula must be numeric!
result
column[number]
required
A numeric column containing the result of evaluating the formula.

Configuration

The following parameters can be used to configure the behaviour of the step by including them in a json object as the last “input” to the step, i.e. step(..., {"param": "value", ...}) -> (output).

Parameters

formula
string
required
The formula to execute. A formula containing basic arithmetic operations and references to column names.
constants
object
Constants to be used in the formula. An object of key-value pairs, where keys refer to names of constants whose values will be available in the formula with a @ prefix. E.g. "constants": {"factor": 1.23} makes it possible to refer to @factor in the formula with given value; also see example(s).
Items
[number, string]
One or more constants. Note that all constant must be of allowed types (number or string).
I