Home > database >  Split 7 digit into separate columns in csv with python
Split 7 digit into separate columns in csv with python

Time:12-02

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

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

CodePudding user response:

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
  • Related