Separate each number using “;” every 8 digits in pandas
Question:
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!
Answers:
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
df1.T.loc[:,0].map(lambda x:";".join([x[i:i+8] for i in range(0,len(x),8)]))
out
X 12345678;12345678;12345678
Y 12345678;12345678;12345678
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!
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
df1.T.loc[:,0].map(lambda x:";".join([x[i:i+8] for i in range(0,len(x),8)]))
out
X 12345678;12345678;12345678
Y 12345678;12345678;12345678