Skip to content
Advertisement

How can I split a cell in a pandas dataframe and keep the delimiter in another column?

persons
John New York
Janet New York
Mike Denver
Michelle Texas

I want to split into 2 columns: person and city. I tried this:

JavaScript

and it gives me this:

JavaScript

What I want is to split by cities and keep the separator in the city column like this:

JavaScript

Advertisement

Answer

You can use regex with a capture group:

JavaScript

Read more on how it works here.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement