datatable.first()

Return the first row for each column from cols.

Parameters

cols
Expr

Input columns.

return
Expr

f-expression having one row, and the same names, stypes and number of columns as in cols.

Examples

first() returns the first column in a frame:

from datatable import dt, f, by, sort, first df = dt.Frame({"A": [1, 1, 2, 1, 2], "B": [None, 2, 3, 4, 5]}) df
AB
01NA
112
223
314
425
dt.first(df)
A
01
11
22
31
42

Within a frame, it returns the first row:

df[:, first(f[:])]
AB
01NA

Of course, you can replicate this by passing 0 to the i section instead:

df[0, :]
AB
01NA

first() comes in handy if you wish to get the first non null value in a column:

df[f.B != None, first(f.B)]
B
02

first() returns the first row per group in a by() operation:

df[:, first(f[:]), by("A")]
AB
01NA
123

To get the first non-null value per row in a by() operation, you can use the sort() function, and set the na_position argument as last:

df[:, first(f[:]), by("A"), sort("B", na_position="last")]
AB
012
123

See Also

  • last() – function that returns the last row.