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
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 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