datatable.last()

Return the last 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

last() returns the last column in a frame:

from datatable import dt, f, by, sort, last df = dt.Frame({"A": [1, 1, 2, 1, 2], "B": [None, 2, 3, 4, None]}) df
AB
int32int32
01NA
112
223
314
42NA
dt.last(df)
B
int32
0NA
12
23
34
4NA

Within a frame, it returns the last row:

df[:, last(f[:])]
AB
int32int32
02NA

The above code can be replicated by passing -1 to the i section instead:

df[-1, :]
AB
int32int32
02NA

Like first(), last() can be handy if you wish to get the last non null value in a column:

df[f.B != None, dt.last(f.B)]
B
int32
04

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

df[:, last(f[:]), by("A")]
AB
int32int32
014
12NA

To get the last non-null value per row in a by() operation, you can use the sort() function, and set the na_position argument as first (this will move the NAs to the top of the column):

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

See Also

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