Depending on your needs, you may use either of the 3 approaches below to convert integers to strings in Pandas DataFrame:
(1) Convert a single DataFrame column using apply(str):
df['DataFrame Column'] = df['DataFrame Column'].apply(str)
(2) Convert a single DataFrame column using astype(str):
df['DataFrame Column'] = df['DataFrame Column'].astype(str)
(3) Convert an entire DataFrame using applymap(str):
df = df.applymap(str)
Let’s now see the steps to apply each of the above approaches in practice.
Steps to Convert Integers to Strings in Pandas DataFrame
Step 1: Collect the Data to be Converted
To start, collect the data that you’d like to convert from integers to strings.
For illustration purposes, let’s use the following data about products and their prices:
Product | Price |
ABC | 350 |
DDD | 370 |
XYZ | 410 |
The goal is to convert the integers under the ‘Price’ column into strings.
Step 2: Create the DataFrame
Next, create the DataFrame to capture the above data in Python.
Here is the code to create the DataFrame for our example:
import pandas as pd data = {'Product': ['ABC','DDD','XYZ'], 'Price': [350,370,410] } df = pd.DataFrame(data) print (df) print (df.dtypes)
Once you run the code in Python, you’ll see that the ‘Price’ column is set to integers:
Product Price
0 ABC 350
1 DDD 370
2 XYZ 410
Product object
Price int64
dtype: object
Step 3: Convert the Integers to Strings in Pandas DataFrame
Finally, you can use the apply(str) template to assist you in the conversion of integers to strings:
df['DataFrame Column'] = df['DataFrame Column'].apply(str)
For our example, the ‘DataFrame column’ that contains the integers is the ‘Price’ column.
Therefore, the full Python code to convert the integers to strings for the ‘Price’ column is:
import pandas as pd data = {'Product': ['ABC','DDD','XYZ'], 'Price': [350,370,410] } df = pd.DataFrame(data) df['Price'] = df['Price'].apply(str) print (df) print (df.dtypes)
Run the code, and you’ll see that the ‘Price’ column is now set to strings (i.e., where the data type is now object):
Product Price
0 ABC 350
1 DDD 370
2 XYZ 410
Product object
Price object
dtype: object
Alternatively, you may use the astype(str) approach to perform the conversion to strings:
df['DataFrame Column'] = df['DataFrame Column'].astype(str)
So the full Python code would look like this:
import pandas as pd data = {'Product': ['ABC','DDD','XYZ'], 'Price': [350,370,410] } df = pd.DataFrame(data) df['Price'] = df['Price'].astype(str) print (df) print (df.dtypes)
As before, you’ll see that the ‘Price’ column now reflects strings:
Product Price
0 ABC 350
1 DDD 370
2 XYZ 410
Product object
Price object
dtype: object
Convert the Entire DataFrame to Strings
Let’s say that you have more than a single column that you’d like to convert from integers to strings.
For example, let’s suppose that you have the following dataset with 3 columns:
Product | Price | Original Cost |
ABC | 350 | 200 |
DDD | 370 | 230 |
XYZ | 410 | 280 |
The goal is to convert the last two columns (i.e., the ‘Price’ and ‘Original Cost’ columns) from integers to strings.
Here is how DataFrame would look like:
import pandas as pd data = {'Product': ['ABC','DDD','XYZ'], 'Price': [350,370,410], 'Original Cost': [200,230,280] } df = pd.DataFrame(data) print (df) print (df.dtypes)
Run the code, and you’ll see that the last two columns are currently set to integers:
Product Price Original Cost
0 ABC 350 200
1 DDD 370 230
2 XYZ 410 280
Product object
Price int64
Original Cost int64
dtype: object
In that case, you may use applymap(str) to convert the entire DataFrame to strings:
df = df.applymap(str)
Here is the complete code for our example:
import pandas as pd data = {'Product': ['ABC','DDD','XYZ'], 'Price': [350,370,410], 'Original Cost': [200,150,100] } df = pd.DataFrame(data) df = df.applymap(str) print (df) print (df.dtypes)
Run the code, and you’ll see that all the columns in the DataFrame are now strings:
Product Price Original Cost
0 ABC 350 200
1 DDD 370 150
2 XYZ 410 100
Product object
Price object
Original Cost object
dtype: object
You may also wish to check the following tutorials that review the steps to convert: