I'm trying to format a dataframe column by adding hyphens to make it easier to read. My dataframe looks like this:
Parcel No.
Property A 0622280070620
Property B 123456789345
Property C 123450694568
my desired outcome is:
Parcel No.
Property A 06-2228-007-0620
Property B 12-3456-789-3452
Property C 13-3450-694-3568
Can you help me figure this out?
CodePudding user response:
Assuming your numbers always have exactly 13 digits:
df = pd.DataFrame({"Parcel No.": [1111111111111, 2222222222222, 3333333333333]},
index=["Property A", "Property B", "Property C"])
df["Parcel No."] = df["Parcel No."].astype(str).apply(lambda num: f"{num[:2]}-{num[2:6]}-{num[6:9]}-{num[9:]}")
print(df)
> Parcel No.
Property A 11-1111-111-1111
Property B 22-2222-222-2222
Property C 33-3333-333-3333
CodePudding user response:
You can try str.replace
if your number length is fixed
df['hypen'] = (df['Parcel No.'].astype(str)
.str.replace(r'(\d{2})(\d{4})(\d{3})(\d{4})', r'\1-\2-\3-\4', regex=True))