Regular expression Replace of substring of a column in pandas python

Regular expression Replace of substring of a column in pandas python can be done by replace() function with Regex argument. Let’s see how to

  •  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

Regular expression Replace of substring of a column in pandas python 1

 

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

Regular expression Replace of substring of a column in pandas python 2

 

Replace last character of column using regular expression:

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

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

so the resultant dataframe will be

Regular expression Replace of substring of a column in pandas python 3

 

Regular expression Replace of substring of a column in pandas python                                                                                                          n Regular expression Replace of substring of a column in pandas python

Author

  • Sridhar Venkatachalam

    With close to 10 years on Experience in data science and machine learning Have extensively worked on programming languages like R, Python (Pandas), SAS, Pyspark.

    View all posts