Python 导出pandas DataFrame时如何删除列名行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19781609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you remove the column name row when exporting a pandas DataFrame?
提问by cmgerber
Say I import the following Excel spreadsheet into a dataframe:
假设我将以下 Excel 电子表格导入到数据框中:
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?
如何删除列名行(在这种情况下Val1, Val2, Val3
),以便我可以导出没有列名的 csv,只有数据?
I have tried df.drop()
and df.ix[1:]
and have not been successful with either.
我已经尝试过df.drop()
,但df.ix[1:]
都没有成功。
采纳答案by Nilani Algiriyage
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
.
您可以header=False
使用index=False
. 如果需要,您还可以使用sep
.
CSV example with no header row, omitting the header row:
没有标题行的 CSV 示例,省略标题行:
df.to_csv('filename.csv', header=False)
TSV (tab-separated) example, omitting the index column:
TSV(制表符分隔)示例,省略索引列:
df.to_csv('filename.tsv', sep='\t', index=False)
回答by cmgerber
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.
这告诉熊猫写一个没有标题的 csv 文件。你可以用 df.to_excel 做同样的事情。
回答by Pearl Philip
If you pass header=False, you will get this error
如果你传递header=False,你会得到这个错误
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
相反,使用 header=None