get list of values from a pandas category column

Pandas Category Column to a List

In this tutorial, we will look at how to convert a category type column in Pandas to a list with the help of some examples.

How to get the list of values from a Pandas categorical column?

You can use the Pandas series tolist() or the Python built-in list() function to get values from a Pandas column as a list. This method works for category type columns as well. The following is the syntax –

# using pandas series .tolist()
df["Col"].tolist()
# using python list()
list(df["Col"])

We get the column values as a list in the order they appear in the dataframe.

Examples

Let’s look at the usage of the above methods with the help of some examples. First, we’ll create a sample Pandas dataframe that we’ll be using throughout this tutorial.

import pandas as pd

# create a dataframe
df = pd.DataFrame({
        "Name": ["Tim", "Sarah", "Hasan", "Jyoti", "Jack"],
        "Shirt Size": ["Small", "Medium", "Large", "Small", "Large"]
})
# change to category dtype
df["Shirt Size"] = df["Shirt Size"].astype("category")
# display the dataframe
print(df)

Output:

    Name Shirt Size
0    Tim      Small
1  Sarah     Medium
2  Hasan      Large
3  Jyoti      Small
4   Jack      Large

We now have a dataframe containing the name and the respective t-shirt size of some students participating in a university competition.

Note that the “Shirt Size” column is of category type.

Category column to list

Let’s get the list of values in the “Shirt Size” column. We’ll use both the methods mentioned above. First, using the Pandas series tolist() function and then, the Python built-in list() function.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# "Shirt Size" column to list using tolist()
print(df["Shirt Size"].tolist())
# "Shirt Size" column to list using list()
print(list(df["Shirt Size"]))

Output:

['Small', 'Medium', 'Large', 'Small', 'Large']
['Small', 'Medium', 'Large', 'Small', 'Large']

You can see that we get the same results from both the methods, that is, values in the “Shirt Size” column as a list.

Get all possible category values for a categorical column

If, on the other hand, you want to get the list of the possible category values for a categorical column, use the .categories property with the help of the .cat accessor.

Let’s get the allowed category values in the “Shirt Size” column.

# categories for "Shirt Size" column
print(df["Shirt Size"].cat.categories)

Output:

Index(['Large', 'Medium', 'Small'], dtype='object')

You can see that we get the possible categories for the “Shirt Size” column. To convert to above result to a list, use the Python list() function.

For more, refer to our tutorial on – Get List of Categories in Pandas Category Column

You might also be interested in –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top