

pandas.ExcelWriter 是 pandas 提供的一個類別,用於將 DataFrame 物件寫入 Excel 檔案的工具。它可以將 DataFrame 寫入到不同的工作表(sheet)中。
以下是 pandas.ExcelWriter 的主要參數和用法說明:
path: Excel 檔案的路徑或檔案物件。可以是 xls、xlsx 或 ods 格式的檔案。engine: 指定寫入 Excel 檔案所使用的引擎。如果為 None,則會根據檔案格式選擇適合的引擎(預設使用 xlsxwriter 或 openpyxl)。date_format和datetime_format: 用於指定日期和日期時間在 Excel 檔案中的格式。mode: 指定寫入模式,可以是 ‘w’(寫入)或 ‘a’(追加)。storage_options: 儲存選項,用於指定特定儲存連線的額外選項,如 host、port、username、password 等。用於 HTTP(S) URLs 時,會作為標頭選項轉發給 urllib.request.Request;用於其他 URLs(例如以 “s3://” 或 “gcs://” 開頭的)時,則會轉發給 fsspec.open。if_sheet_exists: 在追加模式下,當嘗試寫入已存在的工作表時,可以指定不同的行為(error、new、replace 或 overlay)。engine_kwargs: 傳遞給引擎的額外關鍵字參數,這些參數會被傳遞給相應引擎的相關函數。
以下是一些常見的使用範例:
df1 = pd.DataFrame([[1, 2], [3, 4]], columns=["X", "Y"])
df2 = pd.DataFrame([["A", "B"], ["C", "D"]], columns=["P", "Q"])
with pd.ExcelWriter("output.xlsx") as writer:
df1.to_excel(writer, sheet_name="Sheet1")
df2.to_excel(writer, sheet_name="Sheet2")輸出的xlsx(有兩個分頁):

Sheet2:

推薦hahow線上學習python: https://igrape.net/30afN

![Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算 Python: 字串 str.find(關鍵字[,start][,end]),找不到的話回傳-1,如何找出資料字串中,所有關鍵字的index?詞頻計算](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2022/11/20221122100657_32.png?quality=90&zoom=2&ssl=1&resize=350%2C233)



![Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask Python: 如何求整個 pandas.DataFrame 中的最大值? pandas.DataFrame .max().max() ; 如何求最大值的index, columns? numpy.where(condition, [x, y, ]/) ; condition為一 bool_mask](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2023/04/20230418154049_50.png?quality=90&zoom=2&ssl=1&resize=350%2C233)


![Python 如何用pandas.Series.nsmallest() 找到n個與target差距最小的index?再從中找到距離idxmax最近的index?避免誤抓sidelobes的index? targetIdx = (serMean-target_value).abs().nsmallest(n).index.tolist() ;Series切片: .loc[標籤名1:標籤名2] (會含標籤名2) ; .iloc[位置1:位置2] (不含位置2) Python 如何用pandas.Series.nsmallest() 找到n個與target差距最小的index?再從中找到距離idxmax最近的index?避免誤抓sidelobes的index? targetIdx = (serMean-target_value).abs().nsmallest(n).index.tolist() ;Series切片: .loc[標籤名1:標籤名2] (會含標籤名2) ; .iloc[位置1:位置2] (不含位置2)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2023/02/20230222082954_53.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

近期留言