Table of Contents
Converting columns of a Pandas DataFrame into strings can be achieved by using the “astype” function. This function allows the user to specify the data type they want to convert the columns to, in this case, strings. By using this function, the columns of the DataFrame will be converted into strings, allowing for further manipulation and analysis of the data. This process can be particularly useful when dealing with mixed data types in a DataFrame and wanting to perform string operations on the columns. Overall, the “astype” function provides a simple and efficient method for converting columns of a Pandas DataFrame into strings.
Convert Pandas DataFrame Columns to Strings
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 stringsdf = 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 .