Note
Go to the end to download the full example code.
Basic Engine Usage#
This example walks through core df_eval.Engine patterns on a
small in-memory DataFrame.
It covers:
Creating an
EngineEvaluating a single expression with
Engine.evaluate()Defining multiple derived columns with
Engine.apply_schema()Using a few built-in safe functions
Evaluating multiple independent expressions with
Engine.evaluate_many()
import pandas as pd
from df_eval import Engine
Build Input Data#
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
df
Create Engine, Single Expression#
engine = Engine()
single_result = engine.evaluate(df, "a + b")
single_result
0 5
1 7
2 9
dtype: int64
Schema-Driven Derived Columns#
schema = {
"sum": "a + b",
"product": "a * b",
"ratio": "a / b",
"safe_ratio": "safe_divide(a, b)",
}
df_with_derived = engine.apply_schema(df, schema)
df_with_derived
Controlling Output Types with dtypes#
typed_schema = {
"float_sum": "a + b",
"int_product": "a * b",
}
typed_result = engine.apply_schema(
df,
typed_schema,
dtypes={"float_sum": "float64", "int_product": "int32"},
)
typed_result.dtypes
a int64
b int64
float_sum float64
int_product int32
dtype: object
Evaluating Multiple Independent Expressions#
expressions = {
"sum": "a + b",
"product": "a * b",
"avg": "(a + b) / 2",
}
many_results = engine.evaluate_many(df, expressions)
{
name: series.tolist() for name, series in many_results.items()
}
{'a': [1, 2, 3], 'b': [4, 5, 6], 'avg': [2.5, 3.5, 4.5], 'product': [4, 10, 18], 'sum': [5, 7, 9]}
Total running time of the script: (0 minutes 0.926 seconds)