29

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
miradulo
  • 28,857
  • 6
  • 80
  • 93
acb
  • 625
  • 2
  • 10
  • 20
  • Does this answer your question? [xls to csv converter](https://stackoverflow.com/questions/9884353/xls-to-csv-converter) – Tomerikoo Jan 07 '22 at 20:36

2 Answers2

33

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)
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
miradulo
  • 28,857
  • 6
  • 80
  • 93
8

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.

Punnerud
  • 7,195
  • 2
  • 54
  • 44