Extract two columns from a column of strings

Question:

I have a dataframe, which contains strings in this format

Rondonópolis (c/ 5,2%) 3500 7000 2789 4258

I need to create two columns and stay that way. I’ve been trying to use regex but I still can’t

A B
Rondonópolis (c/ 5,2%) 3500 7000 2789 4258
Asked By: Luciano Amaro

||

Answers:

Use str.extract to extract two groups: one of the numbers (four 4-digit numbers) and the other everything preceding those numbers.

df = pd.DataFrame({'my_column': ["Rondonópolis (c/ 5,2%) 3500 7000 2789 4258", "Ponta Grossa 2100 3121 4578 3234"]})
df[['A', 'B']] = df['my_column'].str.extract(r"(.+) (d{4} d{4} d{4} d{4})")

res

Answered By: cottontail