115

Say I import the following Excel spreadsheet into a dataframe:

Val1 Val2 Val3
1     2    3 
5     6    7 
9     1    2

How do I delete the column name row (in this case Val1, Val2, Val3) so that I can export a csv with no column names, just the data?

I have tried df.drop() and df.ix[1:] and have not been successful with either.

smci
  • 32,567
  • 20
  • 113
  • 146
cmgerber
  • 2,199
  • 3
  • 16
  • 15
  • 2
    The *'column name row'* is called the *'header'*. Most read/write commands like `to_csv()` have an option `header` to control it, e.g. `header = None` – smci Apr 24 '20 at 11:06

3 Answers3

219

You can write to csv without the header using header=False and without the index using index=False. If desired, you also can modify the separator using sep.

CSV example with no header row, omitting the header row:

df.to_csv('filename.csv', header=False)

TSV (tab-separated) example, omitting the index column:

df.to_csv('filename.tsv', sep='\t', index=False)
Miles Erickson
  • 2,574
  • 2
  • 17
  • 18
Nilani Algiriyage
  • 32,876
  • 32
  • 87
  • 121
36

Figured out a way to do this:

df.to_csv('filename.csv', header = False)

This tells pandas to write a csv file without the header. You can do the same with df.to_excel.

cmgerber
  • 2,199
  • 3
  • 16
  • 15
  • 2
    No, [the option was changed to `header=None` way back in pandas 0.17 (2015)](https://pandas.pydata.org/pandas-docs/version/0.17.0/whatsnew.html#changes-to-bool-passed-as-header-in-parsers) – smci Apr 24 '20 at 11:08
  • 1
    @smci You might be thinking of `read_csv`. For `to_csv` the type for the `header` parameter is `bool or list of str, default True` – David Gilbertson Jul 07 '22 at 10:46
5

If you pass header=False, you will get this error

TypeError: Passing a bool to header is invalid. Use header=None 
for no header or header=int or list-like of ints to specify the 
row(s) making up the column names

Instead, use header=None

Pearl Philip
  • 883
  • 2
  • 11
  • 16