Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to split string in every 6 digits in a certain column?

In the column ‘id’, I would like to split the string into every 6 digits and add a comma.

df = pd.pivot_table(df, values=['id'], index=['eu_crm'], aggfunc='sum')
df.loc[df[:, 1] for i in range(0, len(['id'], 6)

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Code:

import pandas as pd

data = {'Column 1': ['a', 'b', 'c'],
        'id': [2468938493843983, 345642232, 23343433]}

df = pd.DataFrame(data)

df['id'] = df['id'].astype(str)
df['fromleft'] = [','.join([df['id'][i][j:j+6] for j in range(0, len(df['id'][i]), 6)]) for i in range(len(df))]

print(df)

Output:

  Column 1                id            fromleft
0        a  2468938493843983  246893,849384,3983
1        b         345642232          345642,232
2        c          23343433           233434,33
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading