挑戰:
將新資料框附加到資料框的末尾現有的Excel工作表而不覆蓋現有的data.
解決方案:
在Pandas 版本1.4.0 之前,追加到現有Excel 工作表需要手動將新資料的索引與現有工作表相符並將其保存回來。
改進的熊貓解決方案>= 1.4.0:
Pandas 1.4.0 及更高版本在ExcelWriter 函數中包含「覆蓋」選項,允許附加到現有工作表而不覆蓋現有內容。
appended_data.to_excel(os.path.join(newpath, 'master_data.xlsx'), sheet_name='Sheet1', mode='a', if_sheet_exists='overlay')
熊貓的替代方案 1.4.0:
def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None, **to_excel_kwargs): """ Append a DataFrame [df] to existing Excel file [filename] into [sheet_name] Sheet. If [filename] doesn't exist, then this function will create it. """ writer = pd.ExcelWriter(filename, engine='openpyxl', mode='a') if sheet_name in writer.book.sheetnames: # try to open an existing workbook writer.book = load_workbook(filename) # truncate sheet if startrow is None and sheet_name in writer.book.sheetnames: startrow = writer.book[sheet_name].max_row # index of [sheet_name] sheet idx = writer.book.sheetnames.index(sheet_name) # remove [sheet_name] writer.book.remove(writer.book.worksheets[idx]) # create an empty sheet [sheet_name] using old index writer.book.create_sheet(sheet_name, idx) # copy existing sheets writer.sheets = {ws.title: ws for ws in writer.book.worksheets} else: # file doesn't exist, we are creating a new one startrow = 0 # write out the DataFrame to an ExcelWriter df.to_excel(writer, sheet_name=sheet_name, **to_excel_kwargs) writer.close() writer.save() appended_data.to_excel(os.path.join(newpath, 'master_data.xlsx'), sheet_name='Sheet1', mode='a', if_sheet_exists='overlay')
示例:
import pandas as pd # Existing data existing_df = pd.DataFrame({ 'Name': ['John', 'Mary', 'Bob'], 'Age': [20, 25, 30] }) # New data to append new_df = pd.DataFrame({ 'Name': ['Alice', 'Tom'], 'Age': [35, 40] }) append_df_to_excel('master_data.xlsx', new_df, sheet_name='Sheet1', startrow=existing_df.shape[0] + 1)
其他注意事項:
以上是如何將 Pandas DataFrame 附加到現有 Excel 工作表而不覆寫資料?的詳細內容。更多資訊請關注PHP中文網其他相關文章!