Need to convert integers to strings in pandas DataFrame?
Depending on your needs, you may use either of the 3 methods below to perform the conversion:
(1) Convert a single DataFrame Column using the apply(str) method:
df['DataFrame Column'] = df['DataFrame Column'].apply(str)
(2) Convert a single DataFrame Column using the astype(str) method:
df['DataFrame Column'] = df['DataFrame Column'].astype(str)
(3) Convert an entire DataFrame using the applymap(str) method:
df = df.applymap(str)
Let’s now see the steps to apply each of the above methods 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 example, I gathered the following data about products and their prices:
Product | Price |
ABC | 350 |
DDD | 370 |
XYZ | 410 |
The goal is to convert the integer values under the ‘Price’ column into strings.
Step 2: Create the DataFrame
Next, create the DataFrame to capture the above data in Python.
This 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:
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)
In 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):
Alternatively, you may use the astype(str) method 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:
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:
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:
You may also wish to check the following tutorials that review the steps to convert: