A transaction can carry several tags. The decision that affects the rest of the query is whether each output row represents a transaction or an individual tag. Use a variable-length list for tags, where a transaction can have zero, one, or many values. Use an Arrow fixed-size list when the width is part of the schema, such as two readings per sensor. To count purchases, keep one row per transaction. To count individual tag occurrences, expand the list into rows.

​
Count and test membership

Use cardinality and membership to summarize a collection while retaining its transaction ID. Compare a known empty list with a missing list.

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

tx = DataFrame({
    "id": [1, 2, 3, 4],
    "tags": [["web", "sale", "web"], [], ["store", "sale"], None],
})

tx.project({
    "id": _.id,
    "tag_count": _.tags.cardinality(),
    "online": F.contains(_.tags, "web"),
}).run()
#
idint64
tag_countint64
onlinebool
113True
220False
332False
44nullnull

Three tags can include duplicates. An empty collection has a known count of zero, while a null collection has an unknown count.

​
Select an element

Use index 0 with element_at to select the first element and a negative index to select from the end.

tx.project({
    "id": _.id,
    "first": F.element_at(_.tags, 0),
    "last": F.element_at(_.tags, -1),
}).run()
#
idint64
firstlarge_string
lastlarge_string
11webweb
22nullnull
33storesale
44nullnull

The first position is 0. The empty and missing collections both produce null here. Keep the count alongside the result if you need to distinguish them.

​
Deduplicate within each transaction

A tag attached twice should count once when measuring how many transactions carry that tag. Deduplicate inside each row, then sort the tags for a consistent display order.

unique = tx.project({
    "id": _.id,
    "tags": F.array_sort(F.array_distinct(_.tags)),
})
unique.run()
#
idint64
tagslarge_list<item: large_string>
11['sale', 'web']
22[]
33['sale', 'store']
44null

At this point there is still one row per transaction. The next operation changes that.

​
Expand a list into rows

Expand the unique tags before grouping. Exclude null tags from the metric.

tag_rows = unique.explode("tags").filter(F.is_not_null(_.tags))
tag_rows.agg(["tags"], _.count().alias("transactions")).order_by("tags").run()
#
tagslarge_string
transactionsint64
1sale2
2store1
3web1

Sale occurs on two transactions. Web occurs twice in the source but on just one transaction. Exploding the original tags would count occurrences instead.

​
Read fixed-size arrays from Arrow

Use an Arrow fixed-size list when each input row must contain the same number of elements. Here each reading contains exactly two sensor values. Variable-length tags and fixed-size readings both support array expressions in these examples.

import pyarrow as pa

readings = pa.array([[12, 18], [8, 10]], type=pa.list_(pa.int64(), 2))
sensors = DataFrame.from_arrow(pa.table({"reading": readings}))
sensors.project({
    "peak": F.array_max(_.reading),
    "total": F.array_sum(_.reading),
}).run()
#
peakint64
totalint64
11830
21018

Arrow enforces the two-element constraint when you construct the input. Both reductions return one scalar per sensor reading. Use a variable-length list for collections whose length can vary.

​
Transform elements inside a list

Apply a function to each tag while each transaction still occupies one row. The lambda receives one element of the nested column. Set item_type=str for these tag values. A second lambda can select which elements to keep.

upper_tags = F.array_transform(_.tags, lambda tag: F.upper(tag), item_type=str)
tx.project({
    "id": _.id,
    "labels": upper_tags,
    "sale_tags": F.array_filter(upper_tags, lambda tag: tag == "SALE", item_type=str),
}).run()
#
idint64
labelslarge_list<item: large_string>
sale_tagslarge_list<item: large_string>
11['WEB', 'SALE', 'WEB']['SALE']
22[][]
33['STORE', 'SALE']['SALE']
44nullnull

Compare the two lists on ID 1. Transforming keeps their length. Filtering elements can shorten a list without dropping its transaction. Use explode when the next calculation needs individual tag rows.

​
Build a list from columns

A sensor reports its low and high readings in separate columns. Combine them into a list to calculate the spread within each reading.

sensors = DataFrame({"id": [1, 2], "low": [12, 8], "high": [18, 10]})
paired = sensors.with_columns(F.array(_.low, _.high).alias("pair"))
paired.project({
    "id": _.id, "pair": _.pair,
    "spread": F.array_max(_.pair) - F.array_min(_.pair),
}).run()
#
idint64
pairlarge_list<item: int64>
spreadint64
11[12, 18]6
22[8, 10]2

The two measurements stay on their original row. To compare all measurements across sensors, explode the pair first.

See array functions and DataFrame.explode.