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 :: change figure size pandas 
Python :: CMake Error at 3rdparty/GLFW/CMakeLists.txt:236 (message): The RandR headers were not found 
Python :: print bold python 
Python :: string to date python 
Python :: Python pandas drop any column 
Python :: cv2 grayscale 
Python :: python replace all new lines with space 
Python :: how to make pyautogui faster 
Python :: scipy version check 
Python :: python console pause 
Python :: remove python ubuntu 
Python :: download pip install 
Python :: python check if has attribute 
Python :: selenium press tab python 
Python :: os remove entire folder python 
Python :: convert dataframe to float 
Python :: python delete directory if exists 
Python :: python save figure 
Python :: pycache in gitignore 
Python :: 8 ball responses list python 
Python :: tensorflow check gpu 
Python :: plot to image python 
Python :: How to convert number string or fraction to float 
Python :: hwo much does mano house cost in python 
Python :: how to take list of integer as input in python 
Python :: print colored text python 
Python :: convert pandas series from str to int 
Python :: dj_database_url 
Python :: hwo to separate datetime column into date and time pandas 
Python :: convert pdf to docx python 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =