Is there a way to remove header and split columns with pandas read_csv?

Question:

[Edited: working code at the end]

I have a CSV file with many rows, but only one column. I want to separate the rows’ values into columns.

I have tried

import pandas as pd
    
df = pd.read_csv("TEST1.csv")

final = [v.split(";") for v in df]

print(final)

However, it didn’t work. My CSV file doesn’t have a header, yet the code reads the first row as a header. I don’t know why, but the code returned only the header with the splits, and ignored the remainder of the data.

For this, I’ve also tried

import pandas as pd

df = pd.read_csv("TEST1.csv").shift(periods=1)

final = [v.split(";") for v in df]

print(final)

Which also returned the same error; and

import pandas as pd

df = pd.read_csv("TEST1.csv",header=None) 

final = [v.split(";") for v in df]

print(final)

Which returned

AttributeError: 'int' object has no attribute 'split'

I presume it did that because when header=None or header=0, it appears as 0; and for some reason, the final = [v.split(";") for v in df] is only reading the header.

Also, I have tried inserting a new header:

import pandas as pd

df = pd.read_csv("TEST1.csv")

final = [v.split(";") for v in df]

headerList = ['Time','Type','Value','Size']

pd.DataFrame(final).to_csv("TEST2.csv",header=headerList)

And it did work, partly. There is a new header, but the only row in the csv file is the old header (which is part of the data); none of the other data has transferred to the TEST2.csv file.

Is there any way you could shed a light upon this issue, so I can split all my data?

Many thanks.

EDIT: Thanks to @1extralime, here is the working code:

import pandas as pd

df = pd.read_csv("TEST1.csv",sep=';')

df.columns = ['Time','Type','Value','Size']

df.to_csv("TEST2.csv")
Asked By: mmurer

||

Answers:

Try:

import pandas as pd

df = pd.read_csv('TEST1.csv', sep=';')
df.columns = ['Time', 'Type', 'Value', 'Size']
Answered By: fishmulch