简体   繁体   中英

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

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

pandas.DataFrame.to_csv()的文档中所述,只需将index=False作为关键字参数传递,即可排除行名称。

data_xls.to_csv('csvfile.csv', encoding='utf-8', index=False)

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.

要删除列名称:

data_xls.to_csv("filename.csv",index=False,header=None ,encoding='utf-8')

我刚刚在这里回答了一个可以在另一个 SO 问题中完成工作的函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM