Skip to main content

__ Back to top

__ ` Ctrl ` \+ ` K `

[ ![pandas 2.2.1 documentation - Home](../_static/pandas.svg) ](../index.html)

Site Navigation

  * [ Getting started ](../getting_started/index.html)
  * [ User Guide ](index.html)
  * [ API reference ](../reference/index.html)
  * [ Development ](../development/index.html)
  * [ Release notes ](../whatsnew/index.html)

  * [ __ GitHub  ](https://github.com/pandas-dev/pandas "GitHub")
  * [ __ Twitter  ](https://twitter.com/pandas_dev "Twitter")
  * [ __ Mastodon  ](https://fosstodon.org/@pandas_dev "Mastodon")

Site Navigation

  * [ Getting started ](../getting_started/index.html)
  * [ User Guide ](index.html)
  * [ API reference ](../reference/index.html)
  * [ Development ](../development/index.html)
  * [ Release notes ](../whatsnew/index.html)

  * [ __ GitHub  ](https://github.com/pandas-dev/pandas "GitHub")
  * [ __ Twitter  ](https://twitter.com/pandas_dev "Twitter")
  * [ __ Mastodon  ](https://fosstodon.org/@pandas_dev "Mastodon")

  * [ 10 minutes to pandas ](10min.html)
  * [ Intro to data structures ](dsintro.html)
  * [ Essential basic functionality ](basics.html)
  * [ IO tools (text, CSV, HDF5, â¦) ](io.html)
  * [ PyArrow Functionality ](pyarrow.html)
  * [ Indexing and selecting data ](indexing.html)
  * [ MultiIndex / advanced indexing ](advanced.html)
  * [ Copy-on-Write (CoW) ](copy_on_write.html)
  * [ Merge, join, concatenate and compare ](merging.html)
  * [ Reshaping and pivot tables ](reshaping.html)
  * [ Working with text data ](text.html)
  * [ Working with missing data ](missing_data.html)
  * [ Duplicate Labels ](duplicates.html)
  * Categorical data 
  * [ Nullable integer data type ](integer_na.html)
  * [ Nullable Boolean data type ](boolean.html)
  * [ Chart visualization ](visualization.html)
  * [ Table Visualization ](style.html)
  * [ Group by: split-apply-combine ](groupby.html)
  * [ Windowing operations ](window.html)
  * [ Time series / date functionality ](timeseries.html)
  * [ Time deltas ](timedeltas.html)
  * [ Options and settings ](options.html)
  * [ Enhancing performance ](enhancingperf.html)
  * [ Scaling to large datasets ](scale.html)
  * [ Sparse data structures ](sparse.html)
  * [ Frequently Asked Questions (FAQ) ](gotchas.html)
  * [ Cookbook ](cookbook.html)

  * [ __ ](../index.html)
  * [ User Guide ](index.html)
  * Categorical data 

#  Categorical data  #

This is an introduction to pandas categorical data type, including a short
comparison with Râs ` factor  ` .

` Categoricals  ` are a pandas data type corresponding to categorical
variables in statistics. A categorical variable takes on a limited, and
usually fixed, number of possible values ( ` categories  ` ; ` levels  ` in
R). Examples are gender, social class, blood type, country affiliation,
observation time or rating via Likert scales.

In contrast to statistical categorical variables, categorical data might have
an order (e.g. âstrongly agreeâ vs âagreeâ or âfirst observationâ
vs. âsecond observationâ), but numerical operations (additions, divisions,
â¦) are not possible.

All values of categorical data are either in ` categories  ` or ` np.nan  ` .
Order is defined by the order of ` categories  ` , not lexical order of the
values. Internally, the data structure consists of a ` categories  ` array and
an integer array of ` codes  ` which point to the real value in the `
categories  ` array.

The categorical data type is useful in the following cases:

  * A string variable consisting of only a few different values. Converting such a string variable to a categorical variable will save some memory, see  here  . 

  * The lexical order of a variable is not the same as the logical order (âoneâ, âtwoâ, âthreeâ). By converting to a categorical and specifying an order on the categories, sorting and min/max will use the logical order instead of the lexical order, see  here  . 

  * As a signal to other Python librarie