Python Pandas

Pandas Change the Column Type to String

The DataFrame in Python refers to a “2-Dimensional” data structure that represents the data in rows and columns. After creating DataFrame, sometimes we may need to modify the columns for different purposes, such as changing the data type of the column, adding or removing the index, and so on. To change/convert the type of column, the “dataframe.astype()” method is utilized in Python.

This article will provide you with a step-by-step guide on how to change the column type of Pandas DataFrame to string using the below content:

How to Change the Column Type of Pandas DataFrame to String in Python?

The “dataframe.astype()” method retrieves a new DataFrame by changing the data type to the specified data type. This method can also be utilized to change the specified column type of Pandas DataFrame to string. Here is the syntax:

dataframe.astype(dtype, copy, errors)

In the above syntax:

  • The “dtype” parameter is used to specify the data type or a dictionary consisting of data types for each column.
  • The “copy” parameter is optional and specifies whether a copy should be returned.
  • While the “errors” parameter determines whether errors should be ignored or not

Let’s use this method to perform various examples:

Example 1: Change the Single Column Type of Pandas DataFrame to String

The below code is used to change the single column type of Pandas DataFrame to string:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Henry', 'Anna'],'Id': [45, 55, 65],'Age': [22, 21, 12],'Height': [5.4, 4.5, 4.2]})
print(df, '\n')
print(df.dtypes)
df['Height'] = df['Height'].astype('string')
print('\n',df.dtypes)

Here, the “pandas” library is imported, and DataFrame is created using the “pd.DataFrame()” function. The “df.astype()” method converts the specified type of columns into a string. The “df.dtypes” is used to retrieve the DataFrame columns data types.

Output

The data type of the “Height” column has been changed/converted to a string.

Example 2: Change Multiple Columns Type of Pandas DataFrame to String

Let’s use the following code to change the multiple columns type of DataFrame:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Henry', 'Anna'],'Id': [45, 55, 65],'Age': [22, 21, 12],'Height': [5.4, 4.5, 4.2]})
print(df, '\n')
print(df.dtypes)
df[['Age', 'Height']] = df[['Age', 'Height']].astype('string')
print('\n',df.dtypes)

In the above code, the “df.astype()” method accepts the data type “string” as an argument and changes the data type of the multiple columns of Pandas DataFrame to string type.

Output

The multiple-column type of DataFrame has been changed to string.

Example 3: Change All the Columns Type of Pandas DataFrame to String

Let’s overview the below code that modifies the data types of all columns:

import pandas
df = pandas.DataFrame({'Name': ['Joseph', 'Henry', 'Anna'],'Id': [45, 55, 65],'Age': [22, 21, 12],'Height': [5.4, 4.5, 4.2]})
print(df, '\n')
print(df.dtypes)
df = df.astype('string')
print('\n',df.dtypes)

Here, the “df.astype()” converts all the column types of Pandas DataFrame to string types in Python.

Output

The column type of the entire DataFrame has been changed to string type.

Conclusion

In Python, the “dataframe.astype()” method is utilized to change/convert the data type of the DataFrame columns to the string data type. This method can change single, multiple, or entire DataFrame columns to string type. This blog delivered an in-depth guide on changing/converting the column type to string.

About the author

Haroon Javed

Hi, I'm Haroon. I am an electronics engineer and a technical content writer. I am a tech geek who loves to help people to the best of my knowledge.