Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

pandas convert string from INT TO str

df['DataFrame Column'] = df['DataFrame Column'].astype(str)
Comment

Fastest way to Convert Integers to Strings in Pandas DataFrame

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")
Comment

PREVIOUS NEXT
Code Example
Python :: fixed size list in python 
Python :: convert all sizes to terabytes pandas 
Python :: not intersection list python 
Python :: remove duplicates in json python 
Python :: contextlib closing python file 
Python :: selenium webdriver without opening browser 
Python :: date to timestamp python 
Python :: find a character in a string python last 
Python :: pubg python 
Python :: generating datafraoms using specific row values 
Python :: accuracy for each class 
Python :: correlation meaning 
Python :: python How do you find the middle element of a singly linked list in one pass? 
Python :: python list pop 
Python :: maximize difference codechef 
Python :: how to make a square in python 
Python :: pandas get higher value of column 
Python :: python count elements in sublists 
Python :: ssl socket python 
Python :: read one column pandas 
Python :: is python easy or hard to learn 
Python :: how to make a 2d array in python 
Python :: python range() float 
Python :: package in python 
Python :: python argparse option groups 
Python :: pop up window flutter 
Python :: how to print a message in python 
Python :: urllib_errors 
Python :: pandas order dataframe by column of other dataframe 
Python :: Iterating With for Loops in Python 
ADD CONTENT
Topic
Content
Source link
Name
4+4 =