Data Wrangling with Python Datatable - Select Columns by Data Type#
Link to Source data#
from datatable import dt, f
df = dt.Frame({'a': [1, 2, 1, 2, 1, 2],
'b': [True, False, True, False, True, False],
'c': [1.0, 2.0, 1.0, 2.0, 1.0, 2.0]}
)
df
| a | b | c | |
|---|---|---|---|
| ▪▪▪▪ | ▪ | ▪▪▪▪▪▪▪▪ | |
| 0 | 1 | 1 | 1 |
| 1 | 2 | 0 | 2 |
| 2 | 1 | 1 | 1 |
| 3 | 2 | 0 | 2 |
| 4 | 1 | 1 | 1 |
| 5 | 2 | 0 | 2 |
Select the boolean column
df[:, f[bool]]
| b | |
|---|---|
| ▪ | |
| 0 | 1 |
| 1 | 0 |
| 2 | 1 |
| 3 | 0 |
| 4 | 1 |
| 5 | 0 |
Select the float column
df[:, f[float]]
| c | |
|---|---|
| ▪▪▪▪▪▪▪▪ | |
| 0 | 1 |
| 1 | 2 |
| 2 | 1 |
| 3 | 2 |
| 4 | 1 |
| 5 | 2 |
Exclude integer column
df[:, [dtype.name != "int" for dtype in df.ltypes]]
| b | c | |
|---|---|---|
| ▪ | ▪▪▪▪▪▪▪▪ | |
| 0 | 1 | 1 |
| 1 | 0 | 2 |
| 2 | 1 | 1 |
| 3 | 0 | 2 |
| 4 | 1 | 1 |
| 5 | 0 | 2 |
Resources:
Comments#