导出熊猫数据框时如何删除列名行?

假设我将下面的 Excel 电子表格导入到一个数据框架中:

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

如何删除列名行(在本例中为 Val1, Val2, Val3) ,以便导出没有列名、只有数据的 csv?

我试过 df.drop()df.ix[1:],但都没有成功。

159027 次浏览

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.

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)

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