Reputation: 473
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
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
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
Reputation: 521514
You could use str.replace
here:
df["col_1"] = df["col_1"].str.replace(r'(?<=^.{4})', r'-')
Upvotes: 0