Home > Mobile >  Separate each number using “;” every 8 digits in pandas
Separate each number using “;” every 8 digits in pandas

Time:07-09

All, I have a data frame looks something like this :

Column A : X , Y
Column B : 123456781234567812345678,123456781234567812345678

The goal is to use pandas such that I can split Column B every 8 characters with “;” between numbers.

Final output should like this:

Column A : X ,Y
Column B : 12345678;12345678;12345678
,12345678;12345678;12345678

Is there a way to do this in pandas ?

Thanks for your help in advance!

CodePudding user response:

You can use a regex:

# example input
df = pd.DataFrame({'A': ['X', 'Y'],
                   'B': [123456781234567812345678,
                         123456781234567812345678]})

df['B'] = df['B'].astype(str).str.replace(r'(\d{8})(?<!$)', r'\1;', regex=True)

output (as new column B2 for clarity):

   A                         B                          B2
0  X  123456781234567812345678  12345678;12345678;12345678
1  Y  123456781234567812345678  12345678;12345678;12345678
  • Related