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

Split 7 digit into separate columns in csv with python

Hi I have 100 data in csv file
I want to split 7 digit number into seprate columns with python
My csv file is like this:

A
1234567

Split into new columns:

B C D E F G H
1 2 3 4 5 6 7

I try

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

Splitdigit=  df['A']>str.split(expand=true).add_perfix('A')

>Solution :

If you have strings, you can use:

out = df['A'].astype(str).str.split('(?<=.)(?=.)', expand=True)

Output:

   0  1  2  3  4  5  6
0  1  2  3  4  5  6  7

With the column names:

from string import ascii_uppercase

out = (df['A'].astype(str).str.split('(?<=.)(?=.)', expand=True)
              .rename(columns=dict(enumerate(ascii_uppercase[1:])))
      )

Output:

   B  C  D  E  F  G  H
0  1  2  3  4  5  6  7
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