Say I import the following excel spreadsheet into a df
假设我将以下excel电子表格导入df
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:],并且两者都没有成功。
3 个解决方案
#1
53
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写入没有标题的csv,而使用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)
#2
4
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
#3
3
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.
这告诉pandas写一个没有标题的csv文件。您可以使用df.to_excel执行相同的操作。
#1
53
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写入没有标题的csv,而使用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)
#2
4
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
#3
3
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.
这告诉pandas写一个没有标题的csv文件。您可以使用df.to_excel执行相同的操作。