Python - 使用 Pandas 和 XlsxWriter
Python Pandas 是一個數據分析庫。它可以讀取、篩選和重新排列小型和大型資料集,並將其輸出為多種格式,包括 Excel。
Pandas 使用 XlsxWriter 模組寫入 Excel 檔案。
XlsxWriter 是一個 Python 模組,用於以 XLSX 檔案格式編寫檔案。它可用於將文字、數字和公式寫入多個工作表。此外,它還支援格式化、影像、圖表、頁面設定、自動篩選、條件格式化和許多其他功能。
示例
# import pandas as pd import pandas as pd # Create some Pandas dataframes from some data. df1 = pd.DataFrame({'Data': [11, 12, 13, 14]}) df2 = pd.DataFrame({'Data': [21, 22, 23, 24]}) df3 = pd.DataFrame({'Data': [31, 32, 33, 34]}) df4 = pd.DataFrame({'Data': [41, 42, 43, 44]}) # Create a Pandas Excel writer object using XlsxWriter as the engine. writer = pd.ExcelWriter('pandas_positioning.xlsx', engine ='xlsxwriter') # write and Positioning the dataframes in the worksheet. # Default position, cell A1. df1.to_excel(writer, sheet_name ='Sheet1') df2.to_excel(writer, sheet_name ='Sheet1', startcol = 3) df3.to_excel(writer, sheet_name ='Sheet1', startrow = 6) # It is also possible to write the dataframe without the header and index. df4.to_excel(writer, sheet_name ='Sheet1', startrow = 7, startcol = 4, header = False, index = False) # Close the Pandas Excel writer object and output the Excel file. writer.save()
廣告