datatable.Namespace.__getitem__()

Retrieve column(s) by their indices/names/types.

By “retrieve” we actually mean that an expression is created such that when that expression is used within the DT[i,j] call, it would locate and return the specified column(s).

Parameters

items
int | str | slice | None | type | stype | ltype | list | tuple

The column selector:

int

Retrieve the column at the specified index. For example, f[0] denotes the first column, while f[-1] is the last.

str

Retrieve a column by name.

slice

Retrieve a slice of columns from the namespace. Both integer and string slices are supported.

Note that for string slicing, both the start and stop column names are included, unlike integer slicing, where the stop value is not included. Have a look at the examples below for more clarity.

None

Retrieve no columns (an empty columnset).

type | stype | ltype

Retrieve columns matching the specified type.

list/tuple

Retrieve columns matching the column names/column positions/column types within the list/tuple.

For example, f[0, -1] will return the first and last columns. Have a look at the examples below for more clarity.

return
FExpr

An expression that selects the specified column from a frame.

See also

Notes

Changed in version 1.0.0

f-expressions containing a list/tuple of column names/column positions/column types are accepted within the j selector.

Examples

from datatable import dt, f, by df = dt.Frame({'A': [1, 2, 3, 4], 'B': ["tolu", "sammy", "ogor", "boondocks"], 'C': [9.0, 10.0, 11.0, 12.0]}) df
ABC
int32str32float64
01tolu9
12sammy10
23ogor11
34boondocks12

Select by column position:

df[:, f[0]]
A
int32
01
12
23
34

Select by column name:

df[:, f["A"]]
A
int32
01
12
23
34

Select a slice:

df[:, f[0 : 2]]
AB
int32str32
01tolu
12sammy
23ogor
34boondocks

Slicing with column names:

df[:, f["A" : "C"]]
ABC
int32str32float64
01tolu9
12sammy10
23ogor11
34boondocks12

Note

For string slicing, both the start and stop are included; for integer slicing the stop is not included.

Select by data type:

df[:, f[dt.str32]]
B
str32
0tolu
1sammy
2ogor
3boondocks
df[:, f[float]]
C
float64
09
110
211
312

Select a list/tuple of columns by position:

df[:, f[0, 1]]
AB
int32str32
01tolu
12sammy
23ogor
34boondocks

Or by column names:

df[:, f[("A", "B")]]
AB
int32str32
01tolu
12sammy
23ogor
34boondocks

Note that in the code above, the parentheses are unnecessary, since tuples in python are defined by the presence of a comma. So the below code works as well:

df[:, f["A", "B"]]
AB
int32str32
01tolu
12sammy
23ogor
34boondocks

Select a list/tuple of data types:

df[:, f[int, float]]
AC
int32float64
019
1210
2311
3412

Passing None within an f-expressions returns an empty columnset:

df[:, f[None]]
0
1
2
3