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.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.