I have data frame with one column and I am trying to iterate through each row of that column with function and have values into new column. SO first i tried to run my regex expression on the single string to make sure I get results i expect:
# Importing dependencies
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
import re
# Test the pattern on a s string
s = "64\"X36\"X60\" STACKED STONE AREAWELL BOMAN KEMP"
z = re.search(r"((\d*[\.|-]?\d+(\/\d*)?)\s*((?:cms?
|in|inch|inches|mms?)\b|(?:[\"|\'|\”])|\s?)\s*
[x|X]\s*){0,2}(\d*[\.|-]?\d+(\/\d*)?)\s*((?:cms?
|in|inch|inches|mms?)\b|(?:[\"|\'|\”])|\s?)" , s,
flags=re.I)
print(z.group(0))
And my results are 64"X36"X60" which is exactly what i want to get. However when i apply this in form of the function on the data frame:
def patterns(row):
return re.search(r"((\d*[\.|-]?\d+(\/\d*)?)\s*
((?:cms?|in|inch|inches|mms?)\b|(?:
[\"|\'|\”])|\s?)\s*[x|X]\s*){0,2}(\d*[\.|-]?\d+
(\/\d*)?)\s*((?:cms?|in|inch|inches|mms?)\b|(?:
[\"|\'|\”])|\s?)", row["Description"],
flags=re.I)
# Apply the function to each row
df["Dimensions"] = df.apply(patterns, axis=1)
I get results in format like this:
re.Match object; span=(0, 11), match='52"X36"X72"'
So I think I am not structuring my function correctly. In the sample test when i add
print(z.group(0))
it reads the data from the match element only which is exactly what i nead. Anyone can pin point how do i ajdust the function to give me the same results for each row?
I tried adding .group(0) at the end of the function but this is the error I get once i execute the it with:
df["Dimensions"] = df.apply(patterns, axis=1)

.group(0)at the end of your return code in thepatternsfunction.return re.search(r"((\d ... s?)", row["Description"], flags=re.I).group(0)