Append a character or numeric value to column in pandas python

Append a character or numeric to the column in pandas python can be done by using “+” operator. simple “+” operator is used to concatenate or append a character value to the column in pandas. similarly we can also use the same “+” operator to concatenate or append the numeric value to the start or end of the column. 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
  • Append a numeric or integer value to the start of the column in pandas
  • Append a numeric or integer value to the end of the column in pandas

 

Create dataframe :  Append a character or numeric to the column in pandas python

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

 

Append or concatenate a numeric value to start of the column in pandas:

Appending the numeric value to start of the column in pandas is done with “+” operator as shown below.

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

So the resultant dataframe will be

Append a character or numeric value to column in pandas python 4

 

 

Append or concatenate a numeric value to end of the column in pandas:

Appending the numeric value to end of the column in pandas is done with “+” operator as shown below.

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

So the resultant dataframe will be

Append a character or numeric value to column in pandas python 5


Other Related Topics:

for difference between (+) and append() function refer here

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