Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python using pandas to convert xlsx to csv file. How to delete index column?

Tags:

I am using the following code to convert .xlsx files into .csv files.

import pandas as pd data_xls = pd.read_excel('excelfile.xlsx', 'Sheet2', index_col=None) data_xls.to_csv('csvfile.csv', encoding='utf-8') 

The code is working, however I am getting an index column with the cell numbers which I do not want. Is there anyway to not include or remove that index column?

File output

 Unnamed  Data     0   0.99319613     1   0.99319613     2   0.99319613     3   0.99319613     4   0.99319613     5   0.99319613 
like image 379
acb Avatar asked Feb 23 '17 17:02

acb


People also ask

How do I save a pandas DataFrame to CSV without index?

pandas DataFrame to CSV with no index can be done by using index=False param of to_csv() method. With this, you can specify ignore index while writing/exporting DataFrame to CSV file.

How do I remove an index from a DataFrame in Excel?

We can remove the index column in existing dataframe by using reset_index() function. This function will reset the index and assign the index columns start with 0 to n-1. where n is the number of rows in the dataframe.


2 Answers

As noted in the docs for pandas.DataFrame.to_csv(), simply pass index=False as a keyword argument, to exclude row names.

data_xls.to_csv('csvfile.csv', encoding='utf-8', index=False) 
like image 155
miradulo Avatar answered Oct 04 '22 14:10

miradulo


Inspired by miradulo and fix a number conversion problem:

import pandas as pd data_xls = pd.read_excel('excelfile.xlsx', 'Sheet2', dtype=str, index_col=None) data_xls.to_csv('csvfile.csv', encoding='utf-8', index=False) 

Can drop 'Sheet2' if there is one sheet. dtype=str to avoid number conversion.

like image 43
Punnerud Avatar answered Oct 04 '22 16:10

Punnerud