An expression describes a calculation on the input columns. Import _ from chalk to refer to the current row’s columns. You can assign an expression to a Python variable and use it wherever the input has those columns.

from chalk import _
from chalkdf import DataFrame

tx = DataFrame({"user": ["a", "a", "b"], "amount": [12, 8, 30]})

amount = _.amount
with_fee = tx.with_columns((amount + 2).alias("charged"))
eligible = with_fee.filter(_.charged >= 12)
eligible.project({"customer": _.user, "charge": _.charged}).run()
#
customerstring
chargeint64
1a14
2b32

Here with_columns keeps the original columns and adds charged. The predicate passed to filter chooses rows. The mapping passed to project defines every output column. For existing columns that already have the right names, use select("user", "amount").

In an aggregation, the same input column contributes multiple values to each result. For example, tx.agg(["user"], amount.sum().alias("total")) produces one total per user. See Aggregations.

​
Apply a calculation to several named columns

Build a projection from column names when several fields need the same calculation. The Python comprehension constructs one expression per column before the plan runs.

balances = DataFrame({"cash_cents": [125, 300], "credit_cents": [500, 800]})
amounts = {
    name.removesuffix("_cents") + "_dollars": balances.col(name) / 100.0
    for name in balances.column_names if name.endswith("_cents")
}
balances.project(amounts).run()
#
cash_dollarsdouble
credit_dollarsdouble
11.255.0
23.08.0

Each matching column becomes a separate output with its own name. Explicit names make the projected schema visible to the next operation.

See DataFrame.with_columns, DataFrame.project, DataFrame.filter, DataFrame.agg.