How to Convert Integers to Strings in Pandas DataFrame? - GeeksforGeeks

01 Aug, 2020
In this article, we’ll look at different methods to convert an integer into a string in a Pandas dataframe. In Pandas, there are different functions that we can use to achieve this task :
map(str)
astype(str)
apply(str)
applymap(str)
Example 1 : In this example, we’ll convert each value of a column of integers to string using the map(str) function.
# importing pandas as pdimport pandas as pd   # creating a dictionary of integersdict = {'Integers' : [10, 50, 100, 350, 700]}  # creating dataframe from dictionarydf = pd.DataFrame.from_dict(dict)print(df)print(df.dtypes)  print('\n')  # converting each value of column to a stringdf['Integers'] = df['Integers'].map(str)print(df)print(df.dtypes)
Output :
We can see in the above output that before the the datatype was int64 and after the conversion to a string, the datatype is an object which represents a string.
Example 2 : In this example, we’ll convert each value of a column of integers to string using the astype(str) function.
# importing pandas as pdimport pandas as pd   # creating a dictionary of integersdict = {'Integers' : [10, 50, 100, 350, 700]}  # creating dataframe from dictionarydf = pd.DataFrame.from_dict(dict)print(df)print(df.dtypes)  print('\n')  # converting each value of column to a stringdf['Integers'] = df['Integers'].astype(str)  print(df)print(df.dtypes)
Output :
We can see in the above output that before the the datatype was int64 and after the conversion to a string, the datatype is an object which represents a string.
Example 3 : In this example, we’ll convert each value of a column of integers to string using the apply(str) function.
# importing pandas as pdimport pandas as pd   # creating a dictionary of integersdict = {'Integers' : [10, 50, 100, 350, 700]}  # creating dataframe from dictionarydf = pd.DataFrame.from_dict(dict)print(df)print(df.dtypes)  print('\n')  # converting each value of column to a stringdf['Integers'] = df['Integers'].apply(str)print(df)print(df.dtypes)
Output :
We can see in the above output that before the the datatype was int64 and after the conversion to a string, the datatype is an object which represents a string.
Example 4 : All the methods we saw above, convert a single column from an integer to a string. But we can also convert the whole dataframe into a string using the applymap(str) method.
# importing pandas as pdimport pandas as pd   # creating a dictionary of integersdict = {'Roll No.' : [1, 2, 3, 4, 5], 'Marks':[79, 85, 91, 81, 95]}  # creating dataframe from dictionarydf = pd.DataFrame.from_dict(dict)print(df)print(df.dtypes)  print('\n')  # converting each value of column to a stringdf = df.applymap(str)print(df)print(df.dtypes)
Output :
We can see in the above output that before the the datatype was int64 and after the conversion to a string, the datatype is an object which represents a string.
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Python | Pandas dataframe.groupby()
Defaultdict in Python
Python | Get unique values from a list
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python