datatable.last()

Return the last row for an Expr. If cols is an iterable, simply return the last element.

Parameters

cols
FExpr | iterable

Input columns or an iterable.

return
Expr | ...

One-row f-expression that has the same names, stypes and number of columns as cols. For an iterable the last element is returned.

Examples

Function last() called on a frame, that is an iterable of columns, returns the last column:

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

Called on a set of columns, last() returns the last row:

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

The same could also be achieved by passing -1 to the row selector:

DT[-1, :]
AB
int32int32
02NA

To get the last non-missing value in a column, one should additionally employ a corresponding i-filter:

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

Function last() is group-aware, meaning that it returns the last row per group in a by() context:

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

To get the last non-missing value per group, one should first filter out all the missing values from the column in question:

DT[f.B != None, :][:, last(f.B), by("A")]
AB
int32int32
014
123

Note

Filtering out missing values in the row selector will not work in a general case, e.g. when one needs to find the last non-missing values in several columns.

See Also

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