Extracting the substring of the column in pandas python can be done by using extract function with regular expression in it. Let’s see how to
- Extract the substring of the column in pandas python.
With examples
Syntax: dataframe.column.str.extract(r’regex’)
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
Extract substring of a column in pandas:
We have extracted the last word of the state column using regular expression and stored in other column.
df1['State_code'] = df1.State.str.extract(r'\b(\w+)$', expand=True) print(df1)
so the resultant dataframe will be