Append a character or string to the column in pandas python

Append a character or string to the column in pandas python can be done by using “+” operator. Let’s see how to

  • Append a character or string to start of the column in pandas
  • Append a character or string to end of the column in pandas

First let’s create a dataframe

import pandas as pd
import numpy as np

df1 = {
    'State':['Arizona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL'],
   'Score1':[4,47,55,74,31]}

df1 = pd.DataFrame(df1,columns=['State','Score1'])
print(df1)

df1 will be

Append a character or string to the column in pandas python 1

 

Append a character or string to start of the column in pandas:

Appending the character or string to start of the column in pandas is done with “+” operator as shown below.


df1['State_new'] ='USA-' + df1['State'].astype(str)
print(df1)

So the resultant dataframe will be

Append a character or string to the column in pandas python 2

 

Append a character or string to end of the column in pandas:

Appending the character or string to end of the column in pandas is done with “+” operator as shown below.


df1['State_new'] = df1['State'].astype(str) + '-USA'
print(df1)

So the resultant dataframe will be

Append a character or string to the column in pandas python 3

 

p Append a character or string to the column in pandas python                                                                                                           n Append a character or string to the column in pandas python