Fastest way to Convert Integers to Strings in Pandas DataFrame

Pandas – An open-source library which is used by any programmer. It is a useful library that is used for analyzing the data and for manipulating the data. It is fast, flexible, and understandable, handles the missing data easily. Not only it provides but also enhances the performance of data manipulation and analysis tools with the powerful data structure.

There are four ways of converting integers to strings in pandas.

Method 1: map(str)

frame[‘DataFrame Column’]= frame[‘DataFrame Column’].map(str)

Method 2: apply(str)

frame[‘DataFrame Column’]= frame[‘DataFrame Column’].apply(str)

Method 3: astype(str)

frame[‘DataFrame Column’]= frame[‘DataFrame Column’].astype(str)

Method 4: values.astype(str)

frame[‘DataFrame Column’]= frame[‘DataFrame Column’].values.astype(str)

In order to find out the fastest method we find the time taken by each method required for converting integers to the string. The method which requires the minimum time for conversion is considered to be the fastest method.





import pandas as pd
import sys
import time
import numpy as np
print('Version Of Python: ' + sys.version)
print('Version Of Pandas: ' + pd.__version__)
print('Version Of Numpy: ' + np.version.version)
frame1 = pd.DataFrame(np.random.randint(0, 90, size =(5000000, 1)), columns =['random'])
frame2 = pd.DataFrame(np.random.randint(0, 90, size =(5000000, 1)), columns =['random'])
frame3 = pd.DataFrame(np.random.randint(0, 90, size =(5000000, 1)), columns =['random'])
frame4 = pd.DataFrame(np.random.randint(0, 90, size =(5000000, 1)), columns =['random'])
# Using map(str) method
t1 = time.time()
frame1['random'] = frame1['random'].map(str)
output1 = (time.time() - t1)  
print('Time taken in seconds using map(str): ' + str(output1))
# Using apply(str) method
t2 = time.time()
frame2['random'] = frame2['random'].apply(str)
output2 = (time.time() - t2)  
print('Time taken in seconds using apply(str): ' + str(output2))
# Using astype(str) method
t3 = time.time()
frame3['random'] = frame3['random'].astype(str)
output3 = (time.time() - t3)  
print('Time taken in seconds using astype(str): ' + str(output3))
# Using values.astype(str) method
t4 = time.time()
frame4['random'] = frame4['random'].values.astype(str)
output4 = (time.time() - t4)  
print('Time taken in seconds using values.astype(str): ' + str(output4))
l =[output1, output2, output3, output4]
m =['map(str)', 'apply(str)', 'astype(str)', 'values.astype(str)']
# Fastest way to convert into string
minimum = min(l)
k = l.index(minimum)
fastest = m[k]
# It will print the fastest conversion method.
print(fastest+" is the fastest method")




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.