{
"cells": [
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Data Wrangling with Python Datatable - Select Columns by Data Type"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### [Link to Source data](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.select_dtypes.html)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" | a | b | c |
\n",
" | ▪▪▪▪ | ▪ | ▪▪▪▪▪▪▪▪ |
\n",
" \n",
" \n",
" 0 | 1 | 1 | 1 |
\n",
" 1 | 2 | 0 | 2 |
\n",
" 2 | 1 | 1 | 1 |
\n",
" 3 | 2 | 0 | 2 |
\n",
" 4 | 1 | 1 | 1 |
\n",
" 5 | 2 | 0 | 2 |
\n",
" \n",
"
\n",
" \n",
"
\n"
],
"text/plain": [
""
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from datatable import dt, f\n",
"\n",
"df = dt.Frame({'a': [1, 2, 1, 2, 1, 2],\n",
" 'b': [True, False, True, False, True, False],\n",
" 'c': [1.0, 2.0, 1.0, 2.0, 1.0, 2.0]}\n",
")\n",
"\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Select the boolean column"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" | b |
\n",
" | ▪ |
\n",
" \n",
" \n",
" 0 | 1 |
\n",
" 1 | 0 |
\n",
" 2 | 1 |
\n",
" 3 | 0 |
\n",
" 4 | 1 |
\n",
" 5 | 0 |
\n",
" \n",
"
\n",
" \n",
"
\n"
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[:, f[bool]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Select the float column"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" | c |
\n",
" | ▪▪▪▪▪▪▪▪ |
\n",
" \n",
" \n",
" 0 | 1 |
\n",
" 1 | 2 |
\n",
" 2 | 1 |
\n",
" 3 | 2 |
\n",
" 4 | 1 |
\n",
" 5 | 2 |
\n",
" \n",
"
\n",
" \n",
"
\n"
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[:, f[float]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"- Exclude integer column"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n",
" \n",
" | b | c |
\n",
" | ▪ | ▪▪▪▪▪▪▪▪ |
\n",
" \n",
" \n",
" 0 | 1 | 1 |
\n",
" 1 | 0 | 2 |
\n",
" 2 | 1 | 1 |
\n",
" 3 | 0 | 2 |
\n",
" 4 | 1 | 1 |
\n",
" 5 | 0 | 2 |
\n",
" \n",
"
\n",
" \n",
"
\n"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df[:, [dtype.name != \"int\" for dtype in df.ltypes]]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Resources: \n",
"- [ltype](https://datatable.readthedocs.io/en/latest/api/ltype.html#)\n",
"\n",
"- [stypes](https://datatable.readthedocs.io/en/latest/api/ltype/stypes.html#)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Comments\n",
""
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.18"
}
},
"nbformat": 4,
"nbformat_minor": 4
}