[SOLVED] Insert hyphen between chracters in a panda column

Issue

Consider the following data frame

pd.DataFrame.from_dict({'col_1': ["abcdefg", "hellowworld", "stackoverflow", "thankyou"]})

I want to add a hyphen after each 4 character

desired output is

pd.DataFrame.from_dict(data = {'col_1': ["abcd-efg", "hell-owwo-rld", "stac-kove-rflo-w", "than-kyou"]})

Solution

replaces each of the previous group with itself with an added -

df['col_2'] =df['col_1'].str.replace(r'(\w{4})',r'\1-',regex=True).str.strip('\-')

        col_2
0        abcd-efg
1    hell-owworld
2  stac-koverflow
3       than-kyou

or did you want

df['col_2'] =df['col_1'].str.replace(r'(\w{4})',r'\1-',regex=True).str.strip('\-')

        col_1             col_2
0        abcdefg          abcd-efg
1    hellowworld     hell-owwo-rld
2  stackoverflow  stac-kove-rflo-w
3       thankyou        than-kyou

Answered By – wwnde

Answer Checked By – Katrina (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *