.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "11_demos\python_packages\pandas\demo_categoricals.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_11_demos_python_packages_pandas_demo_categoricals.py: Categoricals =============== .. GENERATED FROM PYTHON SOURCE LINES 5-13 .. code-block:: Python import pandas as pd dataFrame = pd.DataFrame( {"id": [1, 2, 3, 4, 5, 6], "raw_grade": ["a", "b", "b", "a", "a", "e"]} ) .. GENERATED FROM PYTHON SOURCE LINES 15-17 .. code-block:: Python dataFrame["grade"] = dataFrame["raw_grade"].astype("category") .. GENERATED FROM PYTHON SOURCE LINES 18-24 .. code-block:: Python dataFrame["grade"].cat.rename_categories(["very good", "good", "very bad"]) dataFrame["grade"] = dataFrame["grade"].cat.set_categories( ["very bad", "bad", "medium", "good", "very good"] ) dataFrame["grade"] .. rst-class:: sphx-glr-script-out .. code-block:: none 0 NaN 1 NaN 2 NaN 3 NaN 4 NaN 5 NaN Name: grade, dtype: category Categories (5, object): ['very bad', 'bad', 'medium', 'good', 'very good'] .. GENERATED FROM PYTHON SOURCE LINES 25-27 .. code-block:: Python dataFrame.sort_values(by="grade") .. raw:: html
id raw_grade grade
0 1 a NaN
1 2 b NaN
2 3 b NaN
3 4 a NaN
4 5 a NaN
5 6 e NaN


.. GENERATED FROM PYTHON SOURCE LINES 28-30 .. code-block:: Python dataFrame.groupby("grade").size() .. rst-class:: sphx-glr-script-out .. code-block:: none C:\Workspace\itom_development_VS2019_Qt5.15.2_x64\itomProject\itom\demo\python_packages\pandas\demo_categoricals.py:28: FutureWarning: The default of observed=False is deprecated and will be changed to True in a future version of pandas. Pass observed=False to retain current behavior or observed=True to adopt the future default and silence this warning. grade very bad 0 bad 0 medium 0 good 0 very good 0 dtype: int64 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.020 seconds) .. _sphx_glr_download_11_demos_python_packages_pandas_demo_categoricals.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: demo_categoricals.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: demo_categoricals.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: demo_categoricals.zip `