datatable.last()¶
Return the last row for an Expr
. If cols
is an iterable,
simply return the last element.
Parameters¶
FExpr
| iterable
Input columns or an iterable.
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]})
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 1 | NA | |
1 | 1 | 5 | |
2 | 2 | 3 | |
3 | 1 | 4 | |
4 | 2 | NA |
last(DT)
B | ||
---|---|---|
int32 | ||
0 | NA | |
1 | 5 | |
2 | 3 | |
3 | 4 | |
4 | NA |
Called on a set of columns, last()
returns the last row:
DT[:, last(f[:])]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 2 | NA |
The same could also be achieved by passing -1
to the row selector:
DT[-1, :]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 2 | NA |
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 | ||
0 | 4 |
Function last()
is group-aware, meaning that it returns the last row
per group in a by()
context:
DT[:, last(f.B), by("A")]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 1 | 4 | |
1 | 2 | NA |
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")]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 1 | 4 | |
1 | 2 | 3 |
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.