Skip to content
Advertisement

Split columns by space or dash – python

I have a pandas df with mixed formatting for a specific column. It contains the qtr and year. I’m hoping to split this column into separate columns. But the formatting contains a space or a second dash between qtr and year.

I’m hoping to include a function that splits the column by a blank space or a second dash.

JavaScript

out:

JavaScript

split by blank

JavaScript

split by second dash

JavaScript

intended output:

JavaScript

Advertisement

Answer

You can use a regular expression with the extract function of the string accessor.

JavaScript

Result

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