
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Fastest Way to Convert Integers to Strings in Pandas DataFrame
In Python there are many methods to convert Integers to strings in Pandas Dataframe like astype() method, apply() method, map() method, list comprehension, etc. Out of all these methods, the fastest way to convert Integers to string in Pandas data frame can be determined by tracking the time taken by each method for conversion. In this article, we will understand how to convert integers to string in a pandas data frame using all four methods and then track the time taken by each for conversion.
Method 1: Using the List comprehension method
In this method, we create a list of string values by iterating over the integer column and converting each integer to a string value using list comprehension.
Example
In the below example, we create a sample pandas data frame containing integer values. To convert these to string values we create a list of string values using list comprehension and assign the list of string values to the integer column.
import pandas as pd # create a sample data frame with an integer column df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]}) # create a list of string values using a list comprehension str_list = [str(i) for i in df['int_column']] # assign the list of string values to the integer column df['int_column'] = str_list # print the data frame print(df)
Output
int_column 0 1 1 2 2 3 3 4 4 5
Method 2: Using the astype() Method
The astype() method converts the whole column from one datatype to another datatype. Each element of the column is then cast from one datatype to another.
Example
In the below example, we created a sample data frame with an integer column and then converted the column to string using astype() function.
import pandas as pd # create a sample data frame with an integer column df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]}) # convert the integer column to a string column df['int_column'] = df['int_column'].astype(str) # print the data frame print(df)
Output
int_column 0 1 1 2 2 3 3 4 4 5
Method 3: Using the apply() method
The apply method applies a function on each element of the column. Using a lambda function we can apply it to each element of the column and convert it from integer to string.
Example
In the below example, we create a sample data frame with an integer column and then define a lambda function to convert integer to string and apply that lambada function to each element of the column.
import pandas as pd # create a sample data frame with an integer column df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]}) # define a lambda function to convert integers to strings int_to_str = lambda x: str(x) # apply the lambda function to the integer column df['int_column'] = df['int_column'].apply(int_to_str) # print the data frame print(df)
Output
int_column 0 1 1 2 2 3 3 4 4 5
Method 4: Using the map() method
The map() method can also be mapped to each element of the column. A lambda function can be created to convert an integer to a string value and mapped to the integer column.
Example
import pandas as pd # create a sample data frame with an integer column df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]}) # define a lambda function to convert integers to strings int_to_str = lambda x: str(x) # map the lambda function to the integer column df['int_column'] = df['int_column'].map(int_to_str) # print the data frame print(df)
Output
int_column 0 1 1 2 2 3 3 4 4 5
Comparing all the four methods
We can write a code that measures the time taken by each method to convert integer to string in the pandas data frame. The method which takes the least time will be the fastest method.
Example
import pandas as pd import time import pandas as pd import time # create a sample data frame with an integer column df = pd.DataFrame({'int_column': [1, 2, 3, 4, 5]*10000}) # Method 1: Using the astype() method start_time = time.time() df['int_column'] = df['int_column'].astype(str) method1_time = time.time() - start_time print("Time taken for Method 1: ", method1_time) # Method 2: Using the apply() method start_time = time.time() int_to_str = lambda x: str(x) df['int_column'] = df['int_column'].apply(int_to_str) method2_time = time.time() - start_time print("Time taken for Method 2: ", method2_time) # Method 3: Using the map() method start_time = time.time() int_to_str = lambda x: str(x) df['int_column'] = df['int_column'].map(int_to_str) method3_time = time.time() - start_time print("Time taken for Method 3: ", method3_time) # Method 4: Using the list comprehension start_time = time.time() str_list = [str(i) for i in df['int_column']] df['int_column'] = str_list method4_time = time.time() - start_time print("Time taken for Method 4: ", method4_time) # Determine the fastest method times = {'Method 1': method1_time, 'Method 2': method2_time, 'Method 3': method3_time, 'Method 4': method4_time} fastest_method = min(times, key=times.get) print("The fastest method is:", fastest_method)
Output
Time taken for Method 1: 0.03693246841430664 Time taken for Method 2: 0.023466110229492188 Time taken for Method 3: 0.02350783348083496 Time taken for Method 4: 0.027480602264404297 The fastest method is: Method 3
The above output shows that the fastest method is apply() method.
Conclusion
The fastest method to convert an Integer to a string in Pandas data frame is apply() method. In this article, we understood all the methods to convert integers to a string in the pandas data frame and compared all the methods to find out the fastest method.