How to convert Pandas DataFrame columns to string type?

In today’s Pandas Data Analysis tutorial i would like to cover the basics of Python DataFrame column conversion to strings.

We’ll touch on several cases:

  1. Converting specific columns to strings using astype.
  2. Exporting a DataFrame to string

Example data

We will start by creating some test data for you to follow along this exercise:

#import Pandas
import pandas as pd

'Define data dictionary
cand_dict = {'city': ['New York', 'Boston', 'Austin'],
              'num_candidates': [10,20,58]}

'Initialize DataFrame
candidates = pd.DataFrame(cand_dict)

Let’s find out the dtypes of our DataFrame columns:

candidates.dtypes

The result will be:

city              object
num_candidates     int64
dtype: object

Convert DataFrame columns to strings

If we would like to convert a specific column to the string type

candidates['city'].astype('string')

We’ll get the following Series as a result:

0    New York
1      Boston
2      Austin
Name: city, dtype: string

Cast DataFrame object to string

We found out beforehand that the city field was interpreted as a Pandas objects. We can cast every column to a specific data type by passing a dictionary of matching column and dtype pairs as highlighted below:

candidates.astype({'city':'string', 'num_candidates':'int32'}).dtypes

And the result that we will get is:

city string num_candidates int32 dtype: object

Export Pandas Dataframe to strings using to_string()

Here’s an example:

# Saving a DataFrame column
print(candidates['city'].to_string())

#Entire DataFrame
print(candidates.to_string())

Example of Renaming a DataFrame column

A common requirement is to rename a column after it being casted to a new data type. Here is a quick snippet that you can use as an example:

cand_2 = candidates.astype({'city':'string', 'num_candidates':'int32'})

cand_2.rename(columns = {'city':'my_city'})