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
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[1], line 1
----> 1 from datatable import dt, f
2
3 df = dt.Frame({'a': [1, 2, 1, 2, 1, 2],
4 'b': [True, False, True, False, True, False],
File /opt/hostedtoolcache/Python/3.11.17/x64/lib/python3.11/site-packages/datatable/__init__.py:181
178 del datatable
180 # This will run only in Jupyter notebook
--> 181 init_styles()
183 options = dt.options.Config(options={}, prefix="")
184 dt.lib._datatable.initialize_options(options)
AttributeError: module 'IPython.core.display' has no attribute 'display'
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#