{
"cells": [
{
"cell_type": "markdown",
"id": "disciplinary-circuit",
"metadata": {},
"source": [
"# _Categorical_ Data Type\n",
"\n",
"\n",
"`Categoricals` can only take on a limited number of possible values (categories) and
\n",
"can be sorted according to the custom order of the categories.\n",
"\n",
"To harness `Categorical` data type in Lets-Plot you can either add a `pandas.Categotical` variable to
\n",
"your `pandas.DataFrame` or annotate any variable in your dataset as `Categorical` using
\n",
"Lets-Plot `as_discrete()` function and the `levels` parameter.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "entire-rapid",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:45:29.796564Z",
"iopub.status.busy": "2024-04-26T11:45:29.796564Z",
"iopub.status.idle": "2024-04-26T11:45:30.823826Z",
"shell.execute_reply": "2024-04-26T11:45:30.823826Z"
}
},
"outputs": [],
"source": [
"import pandas as pd\n",
"\n",
"from lets_plot import *\n",
"from lets_plot.mapping import as_discrete"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "southwest-newcastle",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-26T11:45:30.823826Z",
"iopub.status.busy": "2024-04-26T11:45:30.823826Z",
"iopub.status.idle": "2024-04-26T11:45:30.839582Z",
"shell.execute_reply": "2024-04-26T11:45:30.839582Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"
\n", " | Unnamed: 0 | \n", "manufacturer | \n", "model | \n", "displ | \n", "year | \n", "cyl | \n", "trans | \n", "drv | \n", "cty | \n", "hwy | \n", "fl | \n", "class | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | \n", "1 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "auto(l5) | \n", "f | \n", "18 | \n", "29 | \n", "p | \n", "compact | \n", "
1 | \n", "2 | \n", "audi | \n", "a4 | \n", "1.8 | \n", "1999 | \n", "4 | \n", "manual(m5) | \n", "f | \n", "21 | \n", "29 | \n", "p | \n", "compact | \n", "
2 | \n", "3 | \n", "audi | \n", "a4 | \n", "2.0 | \n", "2008 | \n", "4 | \n", "manual(m6) | \n", "f | \n", "20 | \n", "31 | \n", "p | \n", "compact | \n", "
3 | \n", "4 | \n", "audi | \n", "a4 | \n", "2.0 | \n", "2008 | \n", "4 | \n", "auto(av) | \n", "f | \n", "21 | \n", "30 | \n", "p | \n", "compact | \n", "