使用Pandas从python行明智地编写xls

我已经使用熊猫成功创建了.xlsx文件

  

df = pd.DataFrame([数组列表])

'''
:param data: Data Rows
:param filename: name of the file
:return:
'''
df = pd.DataFrame(data)

# my "Excel" file,which is an in-memory output file (buffer)
# for the new workbook
excel_file = BytesIO()

writer = pd.ExcelWriter(excel_file,engine='xlsxwriter')
df.to_excel(writer,sheet_name='Sheet1_test')

writer.save()
writer.close()

# important step,rewind the buffer or when it is read() you'll get nothing
# but an error message when you try to open your zero length file in Excel
excel_file.seek(0)

# set the mime type so that the browser knows what to do with the file
response = HttpResponse(excel_file.read(),content_type='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')

# set the file name in the Content-Disposition header
response['Content-Disposition'] = 'attachment; filename=' + filename + '.xlsx'

return response

但是我在这里有问题, 没有不必要的sno。我不想要的,该如何删除。

使用Pandas从python行明智地编写xls

sno。作为第一行和第一列,如何删除?

waruut 回答:使用Pandas从python行明智地编写xls

根据文档here

to_excel默认设置索引以写为新列, 使用索引为False

df.to_excel(writer,sheet_name='Sheet1_test',index=False)
,

您可以从此https://medium.com/better-programming/using-python-pandas-with-excel-d5082102ca27媒体中参考。

本文链接:https://www.f2er.com/3161340.html

大家都在问