{ "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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
abc
▪▪▪▪▪▪▪▪▪▪▪▪
0111
1202
2111
3202
4111
5202
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
b
01
10
21
30
41
50
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
c
▪▪▪▪▪▪▪▪
01
12
21
32
41
52
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
bc
▪▪▪▪▪▪▪▪
011
102
211
302
411
502
\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 }