202

I am reading a csv file into pandas. This csv file constists of four columns and some rows, but does not have a header row, which I want to add. I have been trying the following:

Cov = pd.read_csv("path/to/file.txt", sep='\t')
Frame=pd.DataFrame([Cov], columns = ["Sequence", "Start", "End", "Coverage"])
Frame.to_csv("path/to/file.txt", sep='\t')

But when I apply the code, I get the following Error:

ValueError: Shape of passed values is (1, 1), indices imply (4, 1)

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?

Anton Protopopov
  • 24,186
  • 7
  • 69
  • 85
sequence_hard
  • 3,763
  • 9
  • 24
  • 46
  • Here is a different interpretation of your question: Add another header to an existing Dataframe to create a MultiIndex. – cs95 May 24 '19 at 06:44

4 Answers4

324

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

Cov = pd.read_csv("path/to/file.txt", 
                  sep='\t', 
                  names=["Sequence", "Start", "End", "Coverage"])
cs95
  • 274,032
  • 76
  • 480
  • 537
Leb
  • 12,999
  • 7
  • 48
  • 71
  • 11
    You will laugh. I actually tried this, but was unaware of that you have to put the square brackets around the names. Which is logically in retrospect. Thank you! – sequence_hard Dec 04 '15 at 15:49
  • 1
    No worries, we've all done those silly mistakes. I'm guilty of them too. – Leb Dec 04 '15 at 15:50
  • 1
    @Leb, I had exactly the same problem and I tried your solution. I got the headers for the table but the first row was also replaced by the same header names. What shold I do now? – 007mrviper Nov 13 '18 at 18:28
  • it adds more collumns to my data, all with `NaN` attributes. It was because my separator is a empty space. – SalahAdDin Jan 23 '20 at 13:32
  • Is there a reason why if you create a DataFrame the attribute is called one thing and you a csv file it is called something else. There are like 3 different names I believe for this header label attribute depending upon which function you are using. – demongolem Aug 31 '20 at 11:49
156

Alternatively you could read you csv with header=None and then add it with df.columns:

Cov = pd.read_csv("path/to/file.txt", sep='\t', header=None)
Cov.columns = ["Sequence", "Start", "End", "Coverage"]
Anton Protopopov
  • 24,186
  • 7
  • 69
  • 85
19
col_Names=["Sequence", "Start", "End", "Coverage"]
my_CSV_File= pd.read_csv("yourCSVFile.csv",names=col_Names)

having done this, just check it with[well obviously I know, u know that. But still...

my_CSV_File.head()

Hope it helps ... Cheers

Amarth Gûl
  • 913
  • 1
  • 12
  • 29
Bhardwaj Joshi
  • 311
  • 2
  • 3
11

To fix your code you can simply change [Cov] to Cov.values, the first parameter of pd.DataFrame will become a multi-dimensional numpy array:

Cov = pd.read_csv("path/to/file.txt", sep='\t')
Frame=pd.DataFrame(Cov.values, columns = ["Sequence", "Start", "End", "Coverage"])
Frame.to_csv("path/to/file.txt", sep='\t')

But the smartest solution still is use pd.read_excel with header=None and names=columns_list.

romulomadu
  • 547
  • 5
  • 9