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