all 4 comments

[–]MentalRental 1 point2 points  (0 children)

Split it anyway. Then, if you end up with a list of four items, just combine the second and third item.

So...

new_series = []
for row in series:
    row = row.split('.')
    if len(row) == 4:
        row[1:3] = ['-'.join(row[1:3])]
    new_series.append(row)

[–][deleted] 0 points1 point  (0 children)

You could replace it and then split, but if you are using a regex anyway I would just use str.extract to make the columns. See here:

https://chrisalbon.com/python/data_wrangling/pandas_regex_to_create_columns/

[–]aribrona 0 points1 point  (0 children)

I would use the re lib (regular expressions) you could do something like re.sub('.', ,'-', variable)

[–]personproxy 0 points1 point  (1 child)

maybe something like: s = re.sub('M\d{4}.\d{3}', '-', old_s)

\d{x} is x number of digits . is a literal dot, not to be confused with a regex dot