Table of Contents
Pandas DataFrame columns can be converted to strings by using the astype(str) function. This method will convert the column’s values to strings, allowing you to manipulate the data within the column as a string. This can be useful for data cleaning or formatting purposes. Additionally, it can be used to simplify the comparison of two DataFrame objects.
Often you may wish to convert one or more columns in a pandas DataFrame to strings. Fortunately this is easy to do using the built-in pandas function.
This tutorial shows several examples of how to use this function.
Example 1: Convert a Single DataFrame Column to String
Suppose we have the following pandas DataFrame:
import pandas as pd #create DataFrame df = pd.DataFrame({'player': ['A', 'B', 'C', 'D', 'E'], 'points': [25, 20, 14, 16, 27], 'assists': [5, 7, 7, 8, 11]}) #view DataFrame df player points assists 0 A 25 5 1 B 20 7 2 C 14 7 3 D 16 8 4 E 27 11
We can identify the data type of each column by using dtypes:
df.dtypes
player object
points int64
assists int64
dtype: object
We can see that the column “player” is a string while the other two columns “points” and “assists” are integers.
We can convert the column “points” to a string by simply using astype(str) as follows:
df['points'] = df['points'].astype(str)
We can verify that this column is now a string by once again using dtypes:
df.dtypes
player object
points object
assists int64
dtype: object
Example 2: Convert Multiple DataFrame Columns to Strings
We can convert both columns “points” and “assists” to strings by using the following syntax:
df[['points', 'assists']] = df[['points', 'assists']].astype(str)
And once again we can verify that they’re strings by using dtypes:
df.dtypes
player object
points object
assists object
dtype: object
Example 3: Convert an Entire DataFrame to Strings
Lastly, we can convert every column in a DataFrame to strings by using the following syntax:
#convert every column to strings df = df.astype(str) #check data type of each column df.dtypes player object points object assists object dtype: object
You can find the complete documentation for the astype() function .