convert category type column to string type in pandas

Pandas – Convert Category Type Column to String

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 –

# 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:

    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:

📚 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.

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.

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 –


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