Skip to content
Advertisement

Splitting a column into 2 in a csv file using python

I have a .csv file with 100 rows of data displayed like this

“Jim 1234”

“Sam 1235”

“Mary 1236”

“John 1237”

What I’m trying to achieve is splitting the numbers from the names into 2 columns in python

edit*

Using,

    import pandas as pd
    df = pd.read_csv('test.csv', sep='s+')
    df.to_csv('result.csv', index=False)

I managed to get it to display like this in excelenter image description here

However, the numbers still do not show up in column B as I expected.

Advertisement

Answer

Your data have only one column and a tab delimiter:

pd.read_csv('test.csv', quoting=1, header=None, squeeze=True) 
  .str.split('t', expand=True) 
  .to_csv('result.csv', index=False, header=False)
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement