pandas如何分別寫入excel的不同sheet-創(chuàng)新互聯(lián)

這篇文章給大家分享的是有關(guān)pandas如何分別寫入excel的不同sheet的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

創(chuàng)新互聯(lián)建站-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比皇姑網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式皇姑網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋皇姑地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。

pandas可以非常方便的寫數(shù)據(jù)到excel,那么如何寫多個dataframe到不同的sheet呢?

使用pandas.ExcelWriter

import pandas as pd
 
writer = pd.ExcelFile('your_path.xlsx')
 
df1 = pd.DataFrame()
df2 = pd.DataFrame()
 
df1.to_excel(writer, sheet_name='df_1')
df2.to_excel(writer, sheet_name='df_2')
 
writer.save()

網(wǎng)上的大部分答案基本上都是這些內(nèi)容,但是這里有個大坑,你會發(fā)現(xiàn)找不到想要的xlsx文件。

那么問題出在哪?

我們看看ExcelWriter源碼就知道了

class ExcelFile(object):
  """
  Class for parsing tabular excel sheets into DataFrame objects.
  Uses xlrd. See read_excel for more documentation
  Parameters
  ----------
  io : string, path object (pathlib.Path or py._path.local.LocalPath),
    file-like object or xlrd workbook
    If a string or path object, expected to be a path to xls or xlsx file
  engine: string, default None
    If io is not a buffer or path, this must be set to identify io.
    Acceptable values are None or xlrd
  """

這里已經(jīng)說的很清楚了,希望傳入的是excel的路徑,你只傳了個文件名,當(dāng)然找不到了。

而且從這里我們可以看到,pandas.ExcelWriter實質(zhì)上是用xlrd來解析excel的。這個wrapper提供了更簡單的接口。

感謝各位的閱讀!關(guān)于“pandas如何分別寫入excel的不同sheet”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

文章題目:pandas如何分別寫入excel的不同sheet-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://www.muchs.cn/article26/peijg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、微信小程序服務(wù)器托管、移動網(wǎng)站建設(shè)、用戶體驗、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運營