I have a df.LOCATION column which contains text strings. Each string includes text reflecting origin and destination addresses. My goal is to split origin and destinations so I can create two separate columns.
Most of the times, the origin and destination text are separated by a string pattern "to" e.g. "1234 A St. to 9876 B St.". I have used
map(lambda x: re.split(' to ', x), df.LOCATION)
This works great, except in some rows I have more "to" in the text e.g. "From 1234 A St. need to pick up truck to 9876 B St." - in a case like this, I would still like to split into two strings for origin and destination but my code above will return three lists because of the extra ' to ' string I highlighted in bold.
So to address this, I have implemented
map(lambda x: re.split(' to \d+', x), dfJobs.LOCATION))
This correctly searches for strings where I have not only the text 'to' but also any digits which indicates there is a new address following the it i.e. the destination. This is working, except it will actually remove those initial digits in the destination address as well, but I would like to keep them.
In other words, I would like to detect patterns such as the above, and when they are found, split based only on the 'to' portion of the pattern.