user2512443
user2512443

Reputation: 473

Insert hyphen between chracters in a panda column

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"]})

Upvotes: 1

Views: 925

Answers (3)

Adam Blumenthal
Adam Blumenthal

Reputation: 11

An updated way to do first method suggested by @wwnde

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

Upvotes: 0

wwnde
wwnde

Reputation: 26676

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

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521514

You could use str.replace here:

df["col_1"] = df["col_1"].str.replace(r'(?<=^.{4})', r'-')

Upvotes: 0

Related Questions