How to split all names and initials in dataframe stored without whitespace?

Viewed 35

I have a dataframe with a column containing names and initials. Sometimes one cell carries more than one name and they are entered without whitespace separating them, for example -

A B MclearyT WhitebottomIannis Clancy

I want to turn this into

A B Mcleary, T Whitebottom, Iannis Clancy

Over every cell in the column. The only defining thing I can point to is that the capital letter which starts the name is always preceded by a lower-case letter. What will give me the desired result?

1 Answers
import pandas as pd
pd.Series(['A B MclearyT WhitebottomIannis Clancy']).str.replace('([a-z])([A-Z])', r'\1, \2', regex=True)

0    A B Mcleary, T Whitebottom, Iannis Clancy
Related