Replace a substring of a column in pandas python

Replace a substring of a column in pandas python can be done by replace() funtion. Let’s see how to

  •  Replace a substring with another substring in pandas
  • Replace a pattern of substring with another substring using regular expression

With examples

First let’s create a dataframe


import pandas as pd
import numpy as np

#Create a DataFrame
df1 = {
    'State':['zona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL'],
   'Score':[62,47,55,74,31]}

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

df1 will be

Replace a substring of a column in pandas python 1

 

Replace a substring with another substring in pandas

df1.replace(regex=['zona'], value='Arizona')

A substring Zona is replaced with another string Arizona. So the resultant dataframe will be

Replace a substring of a column in pandas python 2

 

Replace space with underscore:

df1.replace(regex=[' '], value='_')

Space  is replaced with underscore (_) . So the resultant dataframe will be

Replace a substring of a column in pandas python 3

 

Replace a pattern of substring using regular expression:

Using regular expression we will replace the first character of the column by substring ‘HE’

df1.replace(regex=['^.'],value='HE')

so the resultant dataframe will be

Replace a substring of a column in pandas python 5

 

p Replace a substring of a column in pandas python                                                                                                         n Replace a substring of a column in pandas python

Author