In this tutorial, we will look at how to convert a category type column in Pandas to a string type column with the help of some examples.
How to convert category to string type in Pandas?
You can use the Pandas astype()
function to change the data type of a column. To convert a category type column to string type, apply the astype()
function on the column and pass 'str'
as the argument. The following is the syntax –
📚 Discover Online Data Science Courses & Programs (Enroll for Free)
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.
# convert pandas column to string type df["Col"] = df["Col"].astype("str")
It changes the type of the column to string.
Examples
Let’s look at some examples of converting category type column(s) to string type in Pandas. First, we will create a 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"], "Gender": ["Male", "Female", "Male", "Female", "Male"] }) # change to category dtype df["Gender"] = df["Gender"].astype("category") # display the dataframe print(df)
Output:
Upskill your career right now →
Name Gender 0 Tim Male 1 Sarah Female 2 Hasan Male 3 Jyoti Female 4 Jack Male
We have a dataframe containing the names and the gender of some students enrolled in a university program.
The “Gender” column in the above dataframe is of category
type.
# display the "Gender" column print(df["Gender"])
Output:
0 Male 1 Female 2 Male 3 Female 4 Male Name: Gender, dtype: category Categories (2, object): ['Female', 'Male']
You can see the values in the above column and the column type.
Let’s convert this column to string using the Pandas astype()
function.
# category column to string df["Gender"] = df["Gender"].astype("str") # display the "Gender" column print(df["Gender"])
Output:
0 Male 1 Female 2 Male 3 Female 4 Male Name: Gender, dtype: object
You can see that the column is now of object
type, a commonly used type for string values in Pandas.
Upskill your career right now →
Let’s look at another example. This time we’ll convert an ordered category type column to a string type column.
# add column for shirt size df["Shirt Size"] = ["M", "S", "M", "M", "L"] # convert the column to ordered category type df["Shirt Size"] = df["Shirt Size"].astype("category") df["Shirt Size"] = df["Shirt Size"].cat.set_categories(["S", "M", "L"], ordered=True) # display the column print(df["Shirt Size"])
Output:
0 M 1 S 2 M 3 M 4 L Name: Shirt Size, dtype: category Categories (3, object): ['S' < 'M' < 'L']
We added an additional column to our dataframe. The “Shirt Size” column contains the t-shirt sizes of the students and is an ordered categorical type column.
Let’s convert the “Shirt Size” column to string type. We’ll use the same syntax as above.
# category column to string df["Shirt Size"] = df["Shirt Size"].astype("str") # display the "Shirt Size" column print(df["Shirt Size"])
Output:
0 M 1 S 2 M 3 M 4 L Name: Shirt Size, dtype: object
You can see that the “Shirt Size” column is now of object
type.
You might also be interested in –
- Pandas – Rename Categories in Category Column
- Count Frequency of Category Values in Pandas
- Pandas – Check If Category is Ordered
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.