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
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 1 | NA | |
1 | 1 | 2 | |
2 | 2 | 3 | |
3 | 1 | 4 | |
4 | 2 | NA |
dt.last(df)
B | ||
---|---|---|
int32 | ||
0 | NA | |
1 | 2 | |
2 | 3 | |
3 | 4 | |
4 | NA |
Within a frame, it returns the last row:
df[:, last(f[:])]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 2 | NA |
The above code can be replicated by passing -1 to the i
section instead:
df[-1, :]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 2 | NA |
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 | ||
0 | 4 |
last()
returns the last row per group in a by()
operation:
df[:, last(f[:]), by("A")]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 1 | 4 | |
1 | 2 | NA |
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")]
A | B | ||
---|---|---|---|
int32 | int32 | ||
0 | 1 | 4 | |
1 | 2 | 3 |
The content on this page is licensed under the Creative Commons Attribution 4.0 License
(CC BY 4.0) ,
and code samples are licensed under the MIT License.