filter
Builtin index · Collections & strings
result = filter(df, mask)
Input: df, mask · Output: result
What it is
df is a Table; mask is a boolean Vec/Mask the same length as df's rows (typically built from a comparison, e.g. df.x > 0).
Overloads
One call shape — this function is not overloaded.
| Code | Input | Output | Description |
result = filter(df, mask) | df Tabledf is a Table.mask boolean, Vec, or Maskmask is a boolean Vec/Mask the same length as df's rows (typically built from a comparison, e.g. | result Table | Returns a Table containing only the rows where mask is true. |
Examples
From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.
function is_even(n)
return n mod 2 == 0
end function
function add2(a, b)
return a + b
end function
print(filter(acc, "is_even")) # (4, 16, 36)
print(fold(acc, "add2", 0)) # 91
output
(4, 16, 36) 91
from agent-guide
df = table(x=[1,2,3,-1,5], y=[10,20,-5,7,2])
pos = filter(df, df.x > 0)
print(pos)
s = sort_by(df, "y")
print(s)
g = table(grp=[1,1,2,2], v=[10,20,30,40])
agg = group_by_agg(g, "grp", "v", "mean")
print(agg)
output
x y
- --
1 10
2 20
3 -5
5 2
x y
-- --
3 -5
5 2
-1 7
1 10
2 20
grp mean_v
... 3 more linesfrom tour
df = DataFrame(x=[1, 2, 3, 4, 5], y=[10, 20, 30, 40, 50], label="sample")
print(nrow(df)) # 5
print(ncol(df)) # 3
tall = filter(df, df.x > 2)
xy = select(tall, "x", "y")
print(xy)
print(describe(df))
output
5
3
x y
- --
3 30
4 40
5 50
stat x y
----- -------- ---------
count 5 5
mean 3 30
std 1.581139 15.811388
min 1 10
25% 2 20
... 3 more linesfrom collections-strings
measurements = table(
x = [-1.5, 0.5, 2.0, -0.25, 3.75],
sensor = ["a", "b", "a", "c", "b"]
)
write_csv(measurements, "measurements.csv")
df = read_csv("measurements.csv")
print(nrow(df))
print(describe(df))
write_csv(filter(df, df.x > 0), "positive_only.csv")
print(read_all_text("positive_only.csv"))
output
5
stat x
----- -------
count 5
mean 0.9
std 2.03562
min -1.5
25% -0.25
50% 0.5
75% 2
max 3.75
x,sensor
0.5,b
2,a
... 1 more linesfrom file-io