I am reading a csv file into pandas. This csv file consists of four columns and some rows, but does not have a header row, which I want to add. I have been trying the following:
JavaScript
x
4
1
Cov = pd.read_csv("path/to/file.txt", sep='t')
2
Frame = pd.DataFrame([Cov], columns = ["Sequence", "Start", "End", "Coverage"])
3
Frame.to_csv("path/to/file.txt", sep='t')
4
But when I apply the code, I get the following Error:
JavaScript
1
2
1
ValueError: Shape of passed values is (1, 1), indices imply (4, 1)
2
What exactly does the error mean? And what would be a clean way in python to add a header row to my csv file/pandas df?
Advertisement
Answer
You can use names
directly in the read_csv
names : array-like, default None List of column names to use. If file contains no header row, then you should explicitly pass header=None
JavaScript
1
4
1
Cov = pd.read_csv("path/to/file.txt",
2
sep='t',
3
names=["Sequence", "Start", "End", "Coverage"])
4