Convert Integer to String in pandas DataFrame Column in Python (4 Examples)

 

In this tutorial, I’ll show how to convert an integer column to the string data type in a pandas DataFrame in the Python programming language.

Table of contents:

Let’s do this:

 

Example Data & Libraries

We first need to import the pandas library, in order to use the functions that are included in the library.

import pandas as pd                                   # Load pandas

Next, we’ll also need to create some data that we can use in the examples below:

data = pd.DataFrame({'x1':range(0, 5),                # Create pandas DataFrame
                     'x2':range(5, 0, - 1),
                     'x3':range(10, 15)})
print(data)                                           # Print pandas DataFrame

 

table 1 DataFrame convert integer string pandas dataframe column python

 

Have a look at the previous table. It shows that our example data has five observations and three columns.

Next, let’s have a look at the data types of the columns in our pandas DataFrame:

print(data.dtypes)                                    # Check data types of columns
# x1    int64
# x2    int64
# x3    int64
# dtype: object

At this point, all variables has the integer class.

 

Example 1: Convert Single pandas DataFrame Column from Integer to String

In this example, I’ll explain how to change the data type of a single column in a pandas DataFrame from integer to string.

For this task, we can apply the astype function as shown below:

data_new1 = data.copy()                               # Create copy of DataFrame
data_new1['x1'] = data_new1['x1'].astype(str)         # Transform integer to string

The previous Python code has created a new pandas DataFrame with updated column classes. Let’s have a look:

print(data_new1.dtypes)                               # Check data types of columns
# x1    object
# x2     int64
# x3     int64
# dtype: object

As you can see, we have switched the data type of the column x1 from integer to object. Note that the pandas library stores character strings as object dtype, i.e. the variable x1 is actually a string.

 

Example 2: Convert Multiple pandas DataFrame Columns from Integer to String

This example illustrates how to transform multiple columns of a pandas DataFrame from the integer to the string data type.

To accomplish this, we can use the astype function once again:

data_new2 = data.copy()                               # Create copy of DataFrame
data_new2 = data_new2.astype({'x2': str, 'x3': str})  # Transform multiple integers to string

Let’s print the column classes of our new pandas DataFrame:

print(data_new2.dtypes)                               # Check data types of columns
# x1     int64
# x2    object
# x3    object
# dtype: object

The data type of the variables x2 and x3 has been adjusted to the object (i.e. string) class.

 

Example 3: Convert All pandas DataFrame Columns from Integer to String

This example illustrates how to parse all column types in a pandas DataFrame from integer to string.

Have a look at the Python code below:

data_new3 = data.copy()                               # Create copy of DataFrame
data_new3 = data_new3.astype(str)                     # Transform all columns to string

And now, let’s have another look at the column data types in our pandas DataFrame:

print(data_new3.dtypes)                               # Check data types of columns
# x1    object
# x2    object
# x3    object
# dtype: object

This time, we have converted each column to the object dtype (i.e. character string).

 

Example 4: Convert pandas DataFrame Column from Integer to String Using apply() Function

Until now, we have used the astype function in all the examples.

In Example 4, in contrast, I’ll illustrate how to use the apply function instead of the astype function to convert an integer column to the string data type.

Consider the Python code below:

data_new4 = data.copy()                               # Create copy of DataFrame
data_new4['x1'] = data_new4['x1'].apply(str)          # Transform integer to string

Now, let’s return the data types of all columns in our data:

print(data_new4.dtypes)                               # Check data types of columns
# x1    object
# x2     int64
# x3     int64
# dtype: object

As you can see, we have converted only the first column x1 to the object dtype (similar to Example 1).

Whether you want to use the astype or the apply function is a matter of taste in most cases. Sometimes, the apply function is considered to be faster than the astype function. So if you are dealing with larger data sets, this might be a significant advantage.

 

Video & Further Resources

In case you need further information on the contents of this tutorial, you may want to watch the following video on my YouTube channel. In the video, I’m explaining the Python programming codes of this article:

 

The YouTube video will be added soon.

 

Furthermore, you might want to have a look at the related Python articles on my homepage:

 

In summary: In this Python tutorial you have learned how to transform an integer column to the string data type in a pandas DataFrame. In case you have further questions, let me know in the comments. Furthermore, don’t forget to subscribe to my email newsletter to receive updates on new articles.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top