A payment processor supplies a free-text memo with each transaction. Preserve the source memo for reconciliation and derive clean fields for grouping, filters, and display. These examples build on the same four transactions.

​
Normalize case and whitespace

Strip padding before lowercasing. Display the original in quotes so the spaces at either end are visible beside the cleaned value.

from chalk import _, functions as F
from chalkdf import DataFrame

tx = DataFrame({
    "id": [1, 2, 3, 4],
    "memo": [" WEB:Order-0042 ", "web:order-0178", "POS:refund-19", None],
})

clean = tx.with_columns(F.lower(F.trim(_.memo)).alias("text"))
quoted = ('"' + _.memo + '"').alias("quoted_memo")
clean.select("id", quoted, "text").run()
#
idint64
quoted_memolarge_string
textlarge_string
11" WEB:Order-0042 "web:order-0042
22"web:order-0178"web:order-0178
33"POS:refund-19"pos:refund-19
44nullnull

A missing memo stays null through both operations. Fill it only when the application needs a display value.

​
Filter by a text prefix

To select all web transactions, match a literal prefix against the clean field.

clean.filter(F.starts_with(_.text, "web:")).select("id", quoted).run()
#
idint64
quoted_memolarge_string
11" WEB:Order-0042 "
22"web:order-0178"

Testing the original field would miss the padded uppercase memo. Filtering on the normalized field still lets you return the original memo.

​
Match a pattern and extract an identifier

The channel is everything before the colon. An order ID has a more specific pattern. Use a capture group to extract just its digits.

parsed = clean.project({
    "id": _.id,
    "channel": F.split_part(_.text, ":", 0),
    "is_order": F.regexp_like(_.text, r"order-[0-9]+"),
    "order_id": F.regexp_extract(_.text, r"order-([0-9]+)", 1),
})
parsed.run()
#
idint64
channellarge_string
is_orderbool
order_idlarge_string
11webTrue0042
22webTrue0178
33posFalsenull
44nullnullnull

The pattern test returns a boolean, while extraction produces a field you can join or group on. Keep order IDs as text to preserve their leading zeros.

​
Redact before displaying

To keep the channel and event type in a support view, replace each run of digits while retaining the original memo on the source frame.

clean.project({
    "id": _.id,
    "display_memo": F.coalesce(F.regexp_replace(_.text, r"[0-9]+", "***"), "(missing)"),
}).run()
#
idint64
display_memolarge_string
11web:order-***
22web:order-***
33pos:refund-***
44(missing)

The fallback applies to the display field. The missing transaction remains distinguishable when you group by the original field.

​
Summarize by channel

Group on the parsed field after removing transactions without a known channel.

parsed.filter(F.is_not_null(_.channel)).agg(
    ["channel"], _.count().alias("transactions")
).order_by("channel").run()
#
channellarge_string
transactionsint64
1pos1
2web2

The two web memos now contribute to the same channel total.

​
Build a display label from parts

Take the channel and the original order identifier from the parsed fields. Join the two with a separator for display.

parsed.filter(_.is_order).project({
    "label": _.channel + "/" + _.order_id,
}).run()
#
labellarge_string
1web/0042
2web/0178

The labels preserve the leading zeros of the captured order IDs. Keep the fields separate for joins on channel or order ID.

​
Slice a field with a fixed layout

Use a slice when a feed specifies the position of a field. In this feed the first three characters identify the issuing office.

codes = DataFrame({"code": ["SFO0042", "LAX0178", None]})
codes.project({
    "code": _.code, "office": F.substr(_.code, 0, 3),
    "serial": F.substr(_.code, 3),
}).run()
#
codestring
officestring
serialstring
1SFO0042SFO0042
2LAX0178LAX0178
3nullnullnull

The start offset is zero-based. Omitting the length takes the rest of the string. Use a capture group when the prefix varies in length.

See string functions and missing data.