Home > other >  How to separate ID number from Column A to Column B in csv via python code?
How to separate ID number from Column A to Column B in csv via python code?

Time:10-19

So basically im stuck as I tried this code but it isn't splitting the names and number. PLs refer to the sample image to understand the desired outcome.

enter image description here

Code that i have tried

enter image description here

CodePudding user response:

Use str.extract:

Suppose this dataframe:

df = pd.DataFrame({'ColA': ['CAIN TAN86092142', 'YEO KIAT JUN81901613']})
print(df)

# Output:
                   ColA
0      CAIN TAN86092142
1  YEO KIAT JUN81901613

Split on the first encountered digit:

out = df['ColA'].str.extract(r'([^\d]*)(\d )') \
                .rename(columns={0: 'Name', 1: 'Number'})
print(out)

# Output:
           Name    Number
0      CAIN TAN  86092142
1  YEO KIAT JUN  81901613

CodePudding user response:

When opening a plain text file (or in this case a plain text csv file) you can use a for loop to go through the file line by line like so (Current as of Python 3):

name = []
id_num = []

file = open('file.csv', 'r')

for f in file:
    f = f.split(',')               # split the data
    name.append(str(f[0]))     # append name to list
    id_num.append(str(f[1]))   # append ID to list
    

Now that you have the data in lists, you can print/store it how you want.

  • Related