วิธีสร้าง Pivot Table ใน Python

บทช่วยสอนสั้นๆ นี้ชี้นำไปยัง สร้าง pivot table ใน Python ด้วยความช่วยเหลือของขั้นตอนโดยละเอียดที่ให้ข้อมูลเกี่ยวกับการกำหนดค่าสภาพแวดล้อมและโฟลว์ของโปรแกรม Python ขณะใช้ Python Excel pivot table จะถูกสร้างขึ้นโดยใช้โค้ดตัวอย่างที่เรียกใช้ได้หลังจากโหลดเวิร์กบุ๊กต้นฉบับที่มีข้อมูลอินพุตสำหรับตาราง Pivot ในตอนท้าย สมุดงานผลลัพธ์จะถูกบันทึกในรูปแบบใดก็ได้ที่ต้องการ เช่น XLSX, XLS ฯลฯ

ขั้นตอนในการสร้าง Pivot Table ใน Python

  1. สร้างสภาพแวดล้อมเพื่อติดตั้ง Aspose.Cells สำหรับ Python ผ่าน Java เข้าไปในโครงการ
  2. โหลดหรือสร้างวัตถุคลาส Workbook ที่มีข้อมูลอินพุตสำหรับตารางเดือย
  3. รับข้อมูลอ้างอิงไปยังคอลเลกชันตารางเดือยในเป้าหมาย worksheet
  4. เพิ่มตารางเดือยในคอลเลกชัน
  5. กำหนดค่าตารางเดือยที่เพิ่มเข้ามาใหม่
  6. เพิ่มฟิลด์ที่ต้องการลงในพื้นที่ตามลำดับในตารางเดือย
  7. บันทึกสมุดงานออกด้วยตารางเดือยในนั้น

ขั้นตอนเหล่านี้ให้คำแนะนำสำหรับ โค้ด Python เพื่อสร้างตาราง Pivot ใน Excel โดยการแชร์ลิงก์ไปยังทรัพยากรการกำหนดค่าสภาพแวดล้อมและลำดับของงานที่ต้องทำใน Python เพื่อให้ได้ฟังก์ชันการทำงาน เป็นคำแนะนำในการเพิ่มเขตข้อมูลไปยังพื้นที่ต่างๆ ในตารางเดือยตามข้อกำหนด เมื่อเตรียม pivot table แล้ว จะบันทึกลงในไฟล์ Excel ในรูปแบบที่ต้องการ

รหัสเพื่อสร้าง Excel Pivot Table ด้วย Python

import jpype
import csv
import asposecells
jpype.startJVM()
from asposecells.api import License, Workbook, PivotFieldType, LoadOptions,FileFormatType
# Instantiate a license to avoid watermark in the output Excel file having pivot table
cellsLicense = License()
cellsLicense.setLicense("Aspose.Cells.lic")
header = ['City', 'Class', 'Fee']
data = [
['Islamabad','Class 1',750],
['Islamabad','Class 4',1000],
['Karachi','Class 1',300],
['Karachi','Class 4',750],
['Karachi','Class 1',2035],
['Karachi','Class 4',2500],
['Islamabad','Class 1',3215]
]
with open('data.csv', 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
# write the header
writer.writerow(header)
# write the data
writer.writerows(data)
# Create a CSV LoadOptions class object
csvLoadOptions = LoadOptions(FileFormatType.CSV)
# Load the CSV data into Workbook class object using the load options
csvWorkbook = Workbook("data.csv",csvLoadOptions)
# Get access to the first sheet for adding pivot table to it
wsPivotTable = csvWorkbook.getWorksheets().get(0)
# Get access to pivot tables collection in the selected sheet
pivotTablesCollection = wsPivotTable.getPivotTables()
# Create the pivot table and save its index
pivotTableIndex = pivotTablesCollection.add("=A1:C8", "A10", "PythonPivotTable")
# Get access to the newly created pivot table
newPivotTable = pivotTablesCollection.get(pivotTableIndex)
# set flag to hide grand totals for rows
newPivotTable.setRowGrand(False)
# Add the first field to the column area of the pivot table
newPivotTable.addFieldToArea(PivotFieldType.COLUMN, 0)
# Add the second field to the row area of the pivot table
newPivotTable.addFieldToArea(PivotFieldType.ROW, 1)
# Add the third field to the data area
newPivotTable.addFieldToArea(PivotFieldType.DATA, 2)
# Saving the Excel file
csvWorkbook.save("NewPivotTable.xlsx")
jpype.shutdownJVM()
บรรทัดโค้ดเหล่านี้ใน Python สร้างตาราง Pivot ของ Excel โดยใช้ข้อมูลในไฟล์ Excel ที่โหลด โปรดทราบว่าคุณสามารถข้ามการสร้างสมุดงานได้หากคุณมีไฟล์ Excel ที่มีข้อมูลอยู่แล้วและเพิ่งโหลด โค้ดนี้ยังแสดงการปรับแต่งตาราง Pivot ที่สร้างขึ้นใหม่ด้วยการซ่อนผลรวมทั้งหมดของแต่ละแถวโดยตั้งค่าสถานะ RowGrand เป็น False ในขณะที่คุณสามารถปรับแต่งอื่นๆ เช่น ตั้งชื่อเรื่องของข้อความแก้ไข ตั้งค่าคอลัมน์ใหญ่ เรียงลำดับรายการแบบกำหนดเอง ฯลฯ

บทความนี้แนะนำเราในการสร้างตารางเดือย หากคุณต้องการอ่านไฟล์ Excel ที่ป้องกันด้วยรหัสผ่าน โปรดดูบทความเรื่อง {{ไฮเปอร์ลิงก์1}}

 ไทย