Using The Pandas Category Data Type (2023)

Using The Pandas Category DataType (1)


In my previous article, I wrote about pandas data types; what they areand how to convert data to the appropriate type. This article will focus on the pandascategorical data type and some of the benefits and drawbacks of usingit.

Pandas Category DataType

To refresh your memory, here is a summary table of the various pandas data types(akadtypes).

Pandas dtype mapping
Pandas dtypePython typeNumPy typeUsage
objectstrstring_, unicode_Text
int64intint_, int8, int16, int32, int64, uint8, uint16, uint32, uint64Integer numbers
float64floatfloat_, float16, float32, float64Floating point numbers
boolboolbool_True/False values
datetime64NAdatetime64[ns]Date and time values
timedelta[ns]NANADifferences between two datetimes
categoryNANAFinite list of text values

This article will focus on categorical data. As a quick refresher, categorical data isdata which takes on a finite number of possible values. For example, if wewere talking about a physical product like a t-shirt, it could have categoricalvariables suchas:

  • Size (X-Small, Small, Medium, Large,X-Large)
  • Color (Red, Black,White)
  • Style (Short sleeve, longsleeve)
  • Material (Cotton,Polyester)

Attributes such as cost, price, quantity are typically integers orfloats.

The key take away is that whether or not a variable is categorical depends on itsapplication. Since we only have 3 colors of shirts, then that is a good categoricalvariable. However, “color” could represent thousands of values in other situationsso it would not be a goodchoice.

There is no hard and fast rule for how many values a categorical value should have.You should apply your domain knowledge to make that determination on your own data sets.In this article, we will look at one approach for identifying categoricalvalues.

The category data type in pandas is a hybrid data type. It looks and behaves like astring in many instances but internally is represented by an array of integers.This allows the data to be sorted in a custom order and to more efficiently storethedata.

At the end of the day why do we care about using categorical values? There are 3 mainreasons:

  • We can define a custom sort order which can improve summarizing and reporting the data.In the example above, “X-Small” < “Small” < “Medium” < “Large” < “X-Large”.Alphabetical sorting would not be able to reproduce thatorder.
  • Some of the python visualization libraries can interpret the categorical data type to apply approrpiate statistical models or plottypes.
  • Categorical data uses less memory which can lead to performanceimprovements.

While categorical data is very handy in pandas. It is not necessary for every type of analysis.In fact, there can be some edge cases where defining a column of data as categorical thenmanipulating the dataframe can lead to some surprising results. Care must be taken tounderstand the data set and the necessary analysis before converting columns tocategorical datatypes.


One of the main use cases for categorical data types is more efficient memory usage.In order to demonstrate, we will use a large data set from the US Centers for Medicare and Medicaid Services.This data set includes a 500MB+ csv file that has information about research paymentsto doctors and hospital in fiscal year2017.

First, set up imports and read in all thedata:

import pandas as pdfrom pandas.api.types import CategoricalDtypedf_raw = pd.read_csv('OP_DTL_RSRCH_PGYR2017_P06292018.csv', low_memory=False)

I have included the low_memory=False parameter in order to surpressthiswarning:

(Video) Introduction to Categorical Data using pandas DtypeWarning: Columns (..) have mixed types. Specify dtype option on import or set low_memory=False.interactivity=interactivity, compiler=compiler, result=result)

Feel free to read more about this parameter in the pandas read_csvdocumentation.

One interesting thing about this data set is that it has over 176 columns but many of themare empty. I found a stack overflow solution to quickly drop all the columns where atleast 90% of the data is empty. I thought this might be handy for others aswell.

Let’s take a look at the size of these various dataframes. Here is the original dataset:
<class 'pandas.core.frame.DataFrame'>RangeIndex: 607865 entries, 0 to 607864Columns: 176 entries, Change_Type to Context_of_Researchdtypes: float64(34), int64(3), object(139)memory usage: 816.2+ MB

The 500MB csv file fills about 816MB of memory. This seems large but even a low-endlaptop has several gigabytes of RAM so we are nowhere near the need for specializedprocessingtools.

Here is the data set we will use for the rest of thearticle:
<class 'pandas.core.frame.DataFrame'>RangeIndex: 607865 entries, 0 to 607864Data columns (total 33 columns):Change_Type 607865 non-null objectCovered_Recipient_Type 607865 non-null object.....Payment_Publication_Date 607865 non-null objectdtypes: float64(2), int64(3), object(28)memory usage: 153.0+ MB

Now that we only have 33 columns, taking 153MB of memory, let’s take a look at whichcolumns might be good candidates for a categorical datatype.

In order to make this a little easier, I created a small helper function to createa dataframe showing all the unique values in acolumn.

unique_counts = pd.DataFrame.from_records([(col, df[col].nunique()) for col in df.columns], columns=['Column_Name', 'Num_Unique']).sort_values(by=['Num_Unique'])

This table highlights a couple of items that will help determine which values should becategorical. First, there is a big jump in unique values once we get above 557 uniquevalues. This should be a useful threshold for this dataset.

In addition, the date fields should not be converted tocategorical.

The simplest way to convert a column to a categorical type is to useastype('category'). We can use a loop to convert all the columns we careabout using astype('category')

cols_to_exclude = ['Program_Year', 'Date_of_Payment', 'Payment_Publication_Date']for col in df.columns: if df[col].nunique() < 600 and col not in cols_to_exclude: df[col] = df[col].astype('category')

If we use to look at the memory usage, we have taken the 153 MB dataframedown to 82.4 MB. This is pretty impressive. We have cut the memory usage almost in halfjust by converting to categorical values for the majority of ourcolumns.

(Video) Pandas - Convert Object Type to Category Type

There is one other feature we can use with categorical data - defining a custom order.To illustrate, let’s do a quick summary of the total payments made by the form ofpayment:

Covered Recipient Physician7.912815e+07
Covered Recipient Teaching Hospital1.040372e+09
Non-covered Recipient Entity3.536595e+09
Non-covered Recipient Individual2.832901e+06

If we want to change the order of the Covered_Recipient_Type, we need todefine a custom CategoricalDtype:

cats_to_order = ["Non-covered Recipient Entity", "Covered Recipient Teaching Hospital", "Covered Recipient Physician", "Non-covered Recipient Individual"]covered_type = CategoricalDtype(categories=cats_to_order, ordered=True)

Then, explicitly re_order thecategory:

df['Covered_Recipient_Type'] = df['Covered_Recipient_Type'].cat.reorder_categories(cats_to_order, ordered=True)

Now, we can see the sort order in effect with thegroupby:

Non-covered Recipient Entity3.536595e+09
Covered Recipient Teaching Hospital1.040372e+09
Covered Recipient Physician7.912815e+07
Non-covered Recipient Individual2.832901e+06

If you have this same type of data file that you will be processing repeatedly,you can specify this conversion when reading the csv by passing a dictionary ofcolumn names and types via the dtype:parameter.

df_raw_2 = pd.read_csv('OP_DTL_RSRCH_PGYR2017_P06292018.csv', dtype={'Covered_Recipient_Type':covered_type})


We’ve shown that the size of the dataframe is reduced by converting values to categoricaldata types. Does this impact other areas of performance? The answer isyes.

Here is an example of a groupby operation on the categorical vs. object data types.First, perform the analysis on the original inputdataframe.

40.3 ms ± 2.38 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

Now, on the dataframe with categoricaldata:

(Video) Pandas Tutorials # 5 : How to handle Categorical data attributes in Pandas

4.51 ms ± 96.7 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

In this case we sped up the code by 10x, going from 40.3 ms to 4.51 ms. You can imaginethat on much larger data sets, the speedup could be evengreater.


Using The Pandas Category DataType (2)

Photo credit: Frans VanHeerden

Categorical data seems pretty nifty. It saves memory and speeds up code, so why notuse it everywhere? Well, Donald Knuth is correct when he warns about prematureoptimization:

The real problem is that programmers have spent far too much time worrying aboutefficiency in the wrong places and at the wrong times; premature optimizationis the root of all evil (or at least most of it) in programming.

In the examples above, the code is faster but it really does not matter when itis used for quick summary actions that are run infrequently. In addition, all the workto figure out and convert to categorical data is probably not worth it for thisdata set and this simpleanalysis.

In addition, categorical data can yield some surprising behaviors in real worldusage. The examples below will illustrate a couple ofissues.

Let’s build a simple dataframe with one ordered categorical variable that representsthe status of the customer. This trivial example will highlight some potentialsubtle errors when dealing with categorical values. It is worth noting that thisexample shows how to use astype() to convert to the ordered category inone step instead of the two step process usedearlier.

import pandas as pdfrom pandas.api.types import CategoricalDtypesales_1 = [{'account': 'Jones LLC', 'Status': 'Gold', 'Jan': 150, 'Feb': 200, 'Mar': 140}, {'account': 'Alpha Co', 'Status': 'Gold', 'Jan': 200, 'Feb': 210, 'Mar': 215}, {'account': 'Blue Inc', 'Status': 'Silver', 'Jan': 50, 'Feb': 90, 'Mar': 95 }]df_1 = pd.DataFrame(sales_1)status_type = CategoricalDtype(categories=['Silver', 'Gold'], ordered=True)df_1['Status'] = df_1['Status'].astype(status_type)

This yields a simple dataframe that looks likethis:

0200150140GoldJones LLC
1210200215GoldAlpha Co
2905095SilverBlue Inc

We can inspect the categorical column in moredetail:

0 Gold1 Gold2 SilverName: Status, dtype: categoryCategories (2, object): [Silver < Gold]

All looks good. We see the data is all there and that Gold is > thenSilver.

Now, let’s bring in another dataframe and apply the same category to the statuscolumn:

(Video) How do I change the data type of a pandas Series?

sales_2 = [{'account': 'Smith Co', 'Status': 'Silver', 'Jan': 100, 'Feb': 100, 'Mar': 70}, {'account': 'Bingo', 'Status': 'Bronze', 'Jan': 310, 'Feb': 65, 'Mar': 80}]df_2 = pd.DataFrame(sales_2)df_2['Status'] = df_2['Status'].astype(status_type)
010010070SilverSmith Co

Hmm. Something happened to our status. If we just look at the column in moredetail:

0 Silver1 NaNName: Status, dtype: categoryCategories (2, object): [Silver < Gold]

We can see that since we did not define “Bronze” as a valid status, we end upwith an NaN value. Pandas does this for a perfectly good reason. It assumesthat you have defined all of the valid categories and in this case, “Bronze” is notvalid. You can just imagine how confusing this issue could be to troubleshoot ifyou were not looking out forit.

This scenario is relatively easy to see but what would you do if you had 100’s of valuesand the data was not cleaned and normalizedproperly?

Here’s another tricky example where you can “lose” the categoryobject:

sales_1 = [{'account': 'Jones LLC', 'Status': 'Gold', 'Jan': 150, 'Feb': 200, 'Mar': 140}, {'account': 'Alpha Co', 'Status': 'Gold', 'Jan': 200, 'Feb': 210, 'Mar': 215}, {'account': 'Blue Inc', 'Status': 'Silver', 'Jan': 50, 'Feb': 90, 'Mar': 95 }]df_1 = pd.DataFrame(sales_1)# Define an unordered categorydf_1['Status'] = df_1['Status'].astype('category')sales_2 = [{'account': 'Smith Co', 'Status': 'Silver', 'Jan': 100, 'Feb': 100, 'Mar': 70}, {'account': 'Bingo', 'Status': 'Bronze', 'Jan': 310, 'Feb': 65, 'Mar': 80}]df_2 = pd.DataFrame(sales_2)df_2['Status'] = df_2['Status'].astype('category')# Combine the two dataframes into 1df_combined = pd.concat([df_1, df_2])
0200150140GoldJones LLC
1210200215GoldAlpha Co
2905095SilverBlue Inc
010010070SilverSmith Co

Everything looks ok but upon further inspection, we’ve lost our category datatype:

0 Gold1 Gold2 Silver0 Silver1 BronzeName: Status, dtype: object

In this case, the data is still there but the type has been converted to an object.Once again, this is pandas attempt to combine the data without throwing errors but notmaking assumptions. If you want to convert to a category data type now, you can useastype('category').


Now that you know about these gotchas, you can watch out for them. But I will givea few guidelines for how I recommend using categorical datatypes:

  1. Do not assume you need to convert all categorical data to the pandas category datatype.
  2. If the data set starts to approach an appreciable percentage of your useable memory, then consider using categorical datatypes.
  3. If you have very significant performance concerns with operations that are executed frequently, lookat using categoricaldata.
  4. If you are using categorical data, add some checks to make sure the data is clean and completebefore converting to the pandas category type. Additionally, check for NaN values aftercombining or convertingdataframes.

I hope this article was helpful. Categorical data types in pandas can be very useful.However, there are a few issues that you need to keep an eye out for so that you do notget tripped up in subsequent processing. Feel free to add any additional tips orquestions in the comments sectionbelow.


  • 6-Dec-2020: Fix typo in groupby example


What is the category data type in pandas? ›

The category data type in pandas is a hybrid data type. It looks and behaves like a string in many instances but internally is represented by an array of integers. This allows the data to be sorted in a custom order and to more efficiently store the data.

What is categorical data in pandas explain with an example? ›

Categorical are a pandas data type that corresponds to the categorical variables in statistics. Such variables take on a fixed and limited number of possible values. For examples – grades, gender, blood group type etc.

Why do we use category Dtype in pandas and not use object Dtype? ›

As shown below, by using 'category' dtype, the execution is much faster (e.g., less than 1/3 of the time taken comparing to using 'object' date type.)

What is Category data example? ›

Categorical variables represent types of data which may be divided into groups. Examples of categorical variables are race, sex, age group, and educational level.

Which type of data is category data? ›

Qualitative data, also known as the categorical data, describes the data that fits into the categories. Qualitative data are not numerical. The categorical information involves categorical variables that describe the features such as a person's gender, home town etc.

How do you handle categorical values in a dataset? ›

How to Deal with Categorical Data for Machine Learning
  1. One-hot Encoding using: Python's category_encoding library. Scikit-learn preprocessing. Pandas' get_dummies.
  2. Binary Encoding.
  3. Frequency Encoding.
  4. Label Encoding.
  5. Ordinal Encoding.

What is the best way to display categorical data? ›

Frequency tables, pie charts, and bar charts are the most appropriate graphical displays for categorical variables.

How do you deal with categorical data? ›

Ways To Handle Categorical Data With Implementation
  1. Nominal Data: The nominal data called labelled/named data. Allowed to change the order of categories, change in order doesn't affect its value. ...
  2. Ordinal Data: Represent discretely and ordered units. Same as nominal data but have ordered/rank.
Sep 9, 2020

What are the 3 types of categorical variables? ›

Categorical variables represent groupings of some kind. They are sometimes recorded as numbers, but the numbers represent categories rather than actual amounts of things. There are three types of categorical variables: binary, nominal, and ordinal variables.

What are the 2 types of categorical variables explain and give examples each? ›

There are two types of categorical variable, nominal and ordinal. A nominal variable has no intrinsic ordering to its categories. For example, gender is a categorical variable having two categories (male and female) with no intrinsic ordering to the categories. An ordinal variable has a clear ordering.

What are the three types of categorical data? ›

Categorical data is divided into two types, nominal and ordinal.

When to use category pandas? ›

Why use categorical datatypes in pandas?
  1. Memory usage — for string columns where there are many repeated values, categories can drastically reduce the amount of memory required to store the data in memory.
  2. Runtime performance — there are optimizations in place which can improve execution speed for certain operations.
Mar 28, 2021

How do pandas deal with categorical variables? ›

The basic strategy is to convert each category value into a new column and assign a 1 or 0 (True/False) value to the column. This has the benefit of not weighting a value improperly. There are many libraries out there that support one-hot encoding but the simplest one is using pandas ' . get_dummies() method.

How to fill categorical missing values in pandas? ›

One approach to fill these missing values can be to replace them with the most common or occurring class. We can do this by taking the index of the most common class which can be determined by using value_counts() method.

What is an example of categorical answer? ›

Examples of categorical data:

Gender (Male, Female) Brand of soaps (Dove, Olay…) Hair color (Blonde, Brunette, Brown, Red, etc.) Survey on a topic “Do you have children?” (Yes or No)

What are two types of data categories? ›

There are two general types of data – quantitative and qualitative and both are equally important. You use both types to demonstrate effectiveness, importance or value.

How many categories data type are there? ›

Most modern computer languages recognize five basic categories of data types: Integral, Floating Point, Character, Character String, and composite types, with various specific subtypes defined within each broad category.

What are the 4 categories of data? ›

Typically, there are four classifications for data: public, internal-only, confidential, and restricted.

How do you deal with categorical variables in data analysis? ›

1) Using the categorical variable, evaluate the probability of the Target variable (where the output is True or 1). 2) Calculate the probability of the Target variable having a False or 0 output. 3) Calculate the probability ratio i.e. P(True or 1) / P(False or 0). 4) Replace the category with a probability ratio.

Which is the best way to encode categorical variables? ›

This means that if your data contains categorical data, you must encode it to numbers before you can fit and evaluate a model. The two most popular techniques are an integer encoding and a one hot encoding, although a newer technique called learned embedding may provide a useful middle ground between these two methods.

Which encoding is best for categorical data? ›

Hash Encoding represents the categorical data into numerical value by the hashing function. Hashing is often used in data encryption or data comparison, but the main part is still similar — transform one feature to another using hashing function.

How do you visualize 3 categorical variables? ›

To visualize a small data set containing multiple categorical (or qualitative) variables, you can create either a bar plot, a balloon plot or a mosaic plot.

How do you visualize a categorical variable in Python? ›

If one of the main variables is “categorical” (divided into discrete groups) it may be helpful to use a more specialized approach to visualization.
Categorical estimate plots:
  1. pointplot() (with kind="point" )
  2. barplot() (with kind="bar" )
  3. countplot() (with kind="count" )

Which plot is best for categorical variables in Python? ›

While the most popular way of representing categorical data is using a bar plot, there are some other visualization types suitable for this purpose.
  1. Bar plot. ...
  2. Stem plot. ...
  3. Pie chart. ...
  4. Treemap. ...
  5. Waffle chart. ...
  6. Word cloud.
Aug 16, 2021

What is categorical data for dummies? ›

Categorical data can take on numerical values (such as “1” indicating male and “2” indicating female), but those numbers don't have mathematical meaning. You couldn't add them together, for example. ( Other names for categorical data are qualitative data, or Yes/No data.

How do you classify categorical variables? ›

There are two major classes of categorical data, nominal and ordinal. In any nominal categorical data attribute, there is no concept of ordering amongst the values of that attribute.

When should you use categorical data? ›

Categorical variables can be used to represent different types of qualitative data. For example: Ordinal data - represents outcomes for which the order of the groups is relevant. Nominal data - represent outcomes for which the order of groups does not matter.

How do you know if a variable is categorical? ›

A categorical variable (sometimes called a nominal variable) is one that has two or more categories, but there is no intrinsic ordering to the categories.

What is an example of a categorical data question? ›

Categorical Data, sometimes called qualitative data, are data whose values describe some characteristic or category. For example, a survey could ask a random group of people: What is your lucky day of the week?

What is an example of a categorical data that is a number? ›

Categorical data examples include personal biodata information—full name, gender, phone number, etc. Numerical data examples include CGPA calculator, interval sale, etc.

Which of the following is an example of categorical data set? ›

Bar graphs are usually used to represent 'categorical data' while the histograms are usually used for 'continuous data'.

Which are the 3 basic categories of data types? ›

Basic Data Types
  • Integer.
  • Double or Real.
  • String.
  • Boolean.
  • Date/Time.
  • Object.
  • Variant.

What are categories in Python? ›

Categorical are a Pandas data type. A string variable consisting of only a few different values. Converting such a string variable to a categorical variable will save some memory. The lexical order of a variable is not the same as the logical order (“one”, “two”, “three”).

What are the 3 types of data types? ›

Most programming languages support basic data types of integer numbers (of varying sizes), floating-point numbers (which approximate real numbers), characters and Booleans.

What are the 5 different categorization of data types? ›

5 data classification types
  • Public data. Public data is important information, though often available material that's freely accessible for people to read, research, review and store. ...
  • Private data. ...
  • Internal data. ...
  • Confidential data. ...
  • Restricted data.
Jul 13, 2021

What are 4 examples of data types? ›

4 Types of Data: Nominal, Ordinal, Discrete, Continuous.

What are the two main categories of data types? ›

There are two general types of data – quantitative and qualitative and both are equally important. You use both types to demonstrate effectiveness, importance or value.


1. How to use groupby() to group categories in a pandas DataFrame
(Chart Explorers)
2. Why Everyone is Talking About Categorical data in pandas | Part - 24 #machinelearningplus #python
(Machine Learning Plus)
3. Python Data Visualization [ Graphing Categorical Data ] Pandas Data Analysis & Statistics Tutorial
4. #33 Pandas: Categorical data - 1 in Python.- 19 | Tutorial
5. Data Science and ML: Cleaning the Text (Categorical) data in Pandas using .str Method
(AI Hack By AK)
6. Python Pandas Tutorial (Tips and Tricks):- Convert Continuous Data to Categorical Data
(Rahul Tiwari)
Top Articles
Latest Posts
Article information

Author: Van Hayes

Last Updated: 02/13/2023

Views: 5489

Rating: 4.6 / 5 (46 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.