Convert numeric column to character in pandas python (integer to string)

Converting numeric column to character in pandas python is accomplished using astype() function. astype() function converts or Typecasts integer column to string column in pandas. Let’s see how to

  • Typecast or convert numeric column to character in pandas python with astype() function.
  • Typecast or convert numeric to character in pandas python with apply() function.

First let’s create a dataframe.

import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
     'Name':['George','Andrea','micheal','maggie','Ravi','Xien','Jalpa'],
   'is_promoted':[0,1,0,0,1,0,1]}

df1 = pd.DataFrame(df1,columns=['Name','is_promoted'])
print(df1)

df1 will be

Convert numeric column to character in pandas 1

Datatypes of df1 will be

Convert numeric column to character in pandas 2

Note: Object datatype of pandas is nothing but character (string) datatype of python

 

Typecast numeric to character column in pandas python:

astype() function converts numeric column (is_promoted) to character column as shown below

 
# Get current data type of columns 

df1['is_promoted'] = df1.is_promoted.astype(str)
df1.dtypes 

Convert numeric column to character in pandas 3
“is_promoted” column is converted from numeric(integer)  to character (object).

 

Typecast numeric to character column in pandas python using apply():

apply() function takes “str” as argument and converts numeric column (is_promoted) to character column as shown below

 
# Get current data type of columns 

df1['is_promoted'] = df1['is_promoted'].apply(str)
df1.dtypes 

Convert numeric column to character in pandas 3
“is_promoted” column is converted from numeric(integer)  to character (object) using apply() function.


Other Related Topics:

for further details on astype() function one can refer this documentation

Convert numeric column to character in pandas python (integer to string) prev                                                                                                           Convert numeric column to character in pandas python (integer to string) next