簡體   English   中英

使用 pandas 從 python 逐行寫入 xls

[英]Writing xls from python row wise using pandas

我已經使用 pandas 成功創建了.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。 我不想要,我該怎么做才能刪除它。

有 SNo。作為第一行和第一列,我如何刪除它?

有 SNo。 作為第一行和第一列,我如何刪除它?

根據此處的文檔

to_excel 默認設置索引寫入為新列,使用索引為 False

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM