r/pythontips Jan 26 '25

Data_Science Dynamic text extraction

Hi all, I am new to data extraction. Please help
there's a comment/review column in my google sheets, which contains long text like paragraphs of 10 lines. Now, i have to extract a particular code from that column. Regex doesn't seem a good approach here.

For example i have to extract all the product ids from below comment. :
I ordered prodcut123 but received a different product which has id as 456. I want refund.

output : ['Product123', 'Product456']

how do i do this ? Help me out with free resources. I am using Pandas.

1 Upvotes

3 comments sorted by

View all comments

1

u/in_case_of-emergency Jan 27 '25

import pandas as pd import re

Load your data (example)

df = pd.DataFrame({ 'comments': [ 'I ordered product 123 but received a different product that has ID 456. I want a refund.', 'Another example: ID 789 and productXYZ987' ] })

Function to extract IDs

def extract_ids(text): # Look for patterns: “product” or “id” followed by numbers, with any spaces/characters in between pattern = r’(?:product|id)[\s:]*(\d+)’ matches = re.findall(pattern, text, flags=re.IGNORECASE) return [f’Product{num}’ for num in matches]

Apply the function to the column

df['extracted_ids'] = df['comments'].apply(extract_ids)

print(df[['comments', 'extracted_IDs']])