Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you remove the column name row when exporting a pandas DataFrame?

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.

like image 354
cmgerber Avatar asked Nov 05 '13 03:11

cmgerber


People also ask

How do you remove column names in Pandas?

Remove Suffix from column names in Pandas You can use the string rstrip() function or the string replace() function to remove suffix from column names.

How do I remove a column header from a data frame?

Just simply put header=False and for eliminating the index using index=False. If you want to learn more about Pandas then visit this Python Course designed by industrial experts.

How do I remove a column header from a DataFrame in Python?

columns attribute to delete a column of the DataFrame based on its index position. Simply pass df. columns[index] to the columns parameter of the DataFrame. drop() .

How do I delete a row label in Pandas?

Pandas provide data analysts a way to delete and filter data frame using . drop() method. Rows can be removed using index label or column name using this method.


2 Answers

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) 
like image 142
Nilani Algiriyage Avatar answered Sep 19 '22 13:09

Nilani Algiriyage


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.

like image 28
cmgerber Avatar answered Sep 21 '22 13:09

cmgerber