I want to extract a specific part of column in pandas and assign it to a new column. The part that I want to extract is not fixed in a string for eg;
MC-ABC 2+0/XPIC 2+0 or MisMatch!MC-ABS 2+0 ::: / XPIC 2+0.
Here I want to extract only XPIC.
I tried using regular expression but could not get the desired result.
>Solution :
You can use str.extract()
import pandas as pd
# Create a sample DataFrame
data = {'column': ['MC-ABC 2+0/XPIC 2+0', 'MisMatch!MC-ABS 2+0 ::: / XPIC 2+0']}
df = pd.DataFrame(data)
# Extract the desired part using regular expression and assign it to a new column
df['new_column'] = df['column'].str.extract(r'(\bXPIC\b)')
# Print the DataFrame
print(df)
column new_column
0 MC-ABC 2+0/XPIC 2+0 XPIC
1 MisMatch!MC-ABS 2+0 ::: / XPIC 2+0 XPIC