亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何將Excel表格保存到原始工作簿中?

如何將Excel表格保存到原始工作簿中?

慕標琳琳 2023-09-05 20:25:34
我有一個這樣的函數:def DuplicateEachRow():        import pandas as pd        import pathlib        full_path = str(pathlib.Path().absolute()) + '\\' + new_loc        df = pd.read_excel(full_path, header=None, sheet_name='Sheet3')        print(df)        # duplicate the rows:        dup_df = pd.concat([df, df], ignore_index=True)        # using openpyxl        with pd.ExcelWriter(new_loc) as writer:            dup_df.to_excel(writer)我需要保留相同的功能,但不是將一張紙寫入新文件。我需要編輯該特定工作表并將其保存回包含其他工作表的工作簿中。編輯(更多解釋):我的工作簿中有 4 張工作表,只有一張工作表(Sheet3)我需要使用上面的功能,然后將其保存回工作簿。這也不起作用,在保存時指定工作表名稱:def DuplicateEachRow():        import pandas as pd        import pathlib        full_path = str(pathlib.Path().absolute()) + '\\' + new_loc        df = pd.read_excel(full_path, header=None, sheet_name='GTL | GWL Disclosures')        print(df)        # duplicate the rows:        dup_df = pd.concat([df, df], ignore_index=True)        # using openpyxl        with pd.ExcelWriter(new_loc) as writer:            dup_df.to_excel(writer, sheet_name='GTL | GWL Disclosures')
查看完整描述

1 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

要在同一個 Excel 中添加新聞表,您必須以追加模式打開文件。看看下面的代碼:


def DuplicateEachRow():

    import pandas as pd

    import pathlib

    full_path = str(pathlib.Path().absolute()) + '\\' + new_loc


    df = pd.read_excel(full_path, header=None, sheet_name='GTL | GWL Disclosures')

    print(df)


    # duplicate the rows:

    # keep the index, so you can sort the rows after

    dup_df = pd.concat([df, df])

    #sort the rows by the index so you have the duplicate one just after the initial one

    dup_df.sort_index(inplace=True)


    # using openpyxl

    #open the file in append mode 

    with pd.ExcelWriter(new_loc, mode='a') as writer:

        #use a new name for the new sheet

        #don't save the header (dataframe columns names) and index (dataframe row names) in the new sheet  

        dup_df.to_excel(writer, sheet_name='Sheet3', header=None, index=None)


查看完整回答
反對 回復 2023-09-05
  • 1 回答
  • 0 關注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號