I'm having trouble finding a straight solution to following problem.
I have a column in a dataframe where I have str items like:
'RosemontCentral'
'Dollard-des-OrmeauxEast'
I want to separate a string at the point where a capital letter starts but not if its preceded by a hyphen.
For example:
'RosemontCentral'
to 'Rosemont Central'
'Dollard-des-OrmeauxEast'
to 'Dollard-des-Ormeaux East'
I have the bellow regex function so far. It does a fairly good job with items such as the first one, where there is no hyphenated words. But, not with the ones that have hyphens. Additionally, the below regex function adds an undesirable leading space at the very beginning of the string. Like the one below.
' Dollard-des- Ormeaux East'
def add_space(Neighborhood):
return re.sub( r"([A-Z])", r" \1", Neighborhood)
df['Neighborhood'] = df['Neighborhood'].apply(add_space)
df
Thank you for your time