pyarrow(開發人員工作原理)
PyArrow 是一個強大的程式庫,提供 Python 接口給 Apache Arrow 框架。 Apache Arrow 是一個用於記憶體資料的跨語言開發平臺。 它指定了一種標準化的語言無關的列式記憶體格式,用於扁平和分層資料的組織,以便在現代硬體上進行高效的分析操作。 PyArrow 基本上是作爲 Python 包實現的 Apache Arrow Python 綁定。 PyArrow 使得不同資料處理系統和編程語言之間的資料交換和互操作變得高效。 在本文的後面,我們還將學習由 Iron Software 開發的 PDF 生成程式庫 IronPDF。
PyArrow 的關鍵功能
列式内存格式:
PyArrow 使用列式記憶體格式,這對於記憶體中的分析操作非常高效。 這種格式允許更好的 CPU 快取利用和向量化操作,使其非常適合資料處理任務。 由於其列式特性,PyArrow 可以高效地讀寫到 Parquet 文件結構。
- 互操作性: PyArrow 的主要優勢之一是能夠在不同編程語言和系統之間促進資料交換,而無需序列化或反序列化。 這在多種語言使用的環境中特別有用,例如資料科學和機器學習。
- 與 Pandas 整合: PyArrow 可以用作 Pandas 的後端,從而實現高效的資料操控和儲存。 從 Pandas 2.0 開始,可以將資料儲存在 Arrow 陣列中而不是 NumPy 陣列中,這可以提高性能,尤其是在處理字串資料時。
- 支持各種資料型別: PyArrow 支持廣泛的資料型別,包括基本型別(整數、浮點數)、複雜型別(Struct、List)和巢狀型別。這使得它在處理不同種類的資料時非常靈活。
- 零拷貝讀取: PyArrow 允許進行零拷貝讀取,這意味着可以從 Arrow 記憶體格式中讀取資料而不進行復制。 這減少了記憶體開銷並提高了性能。
安裝
要安裝 PyArrow,您可以使用 pip 或 conda:
pip install pyarrowpip install pyarrowor
conda install pyarrow -c conda-forgeconda install pyarrow -c conda-forge基本用法
我們正在使用 Visual Studio Code 作為程式編輯器。 首先建立一個新文件,pyarrowDemo.py。
以下是使用 PyArrow 建立表並執行一些基本操作的簡單範例:
import pyarrow as pa
import pyarrow.dataset as pt
# Create a PyArrow table
data = [
pa.array([1, 2, 3]),
pa.array(['a', 'b', 'c']),
pa.array([1.1, 2.2, 3.3])
]
table = pa.Table.from_arrays(data, names=['col1', 'col2', 'col3'])
# Display the table
print(table)import pyarrow as pa
import pyarrow.dataset as pt
# Create a PyArrow table
data = [
pa.array([1, 2, 3]),
pa.array(['a', 'b', 'c']),
pa.array([1.1, 2.2, 3.3])
]
table = pa.Table.from_arrays(data, names=['col1', 'col2', 'col3'])
# Display the table
print(table)程式碼解釋
Python 程式碼使用 PyArrow 從三個陣列 (pa.array) 建立一個表 (pa.Table)。 然後它列印表,顯示名爲 'col1'、'col2' 和 'col3' 的列,每列包含相應的整數、字串和浮點資料。
輸出

与 Pandas 整合
PyArrow 可以與 Pandas 無縫整合以提升性能,特別是在處理大型資料集時。 以下是將 Pandas DataFrame 轉換爲 PyArrow 表的例子:
import pandas as pd
import pyarrow as pa
# Create a Pandas DataFrame
df = pd.DataFrame({
'col1': [1, 2, 3],
'col2': ['a', 'b', 'c'],
'col3': [1.1, 2.2, 3.3]
})
# Convert the DataFrame to a PyArrow Table
table = pa.Table.from_pandas(df)
# Display the table
print(table)import pandas as pd
import pyarrow as pa
# Create a Pandas DataFrame
df = pd.DataFrame({
'col1': [1, 2, 3],
'col2': ['a', 'b', 'c'],
'col3': [1.1, 2.2, 3.3]
})
# Convert the DataFrame to a PyArrow Table
table = pa.Table.from_pandas(df)
# Display the table
print(table)程式碼解釋
Python 程式碼將 Pandas DataFrame 轉換爲 PyArrow 表 (pa.Table),然後列印該表。 DataFrame 包含三個列 (col1, col2, col3),包含整数、字串和浮点数据。
輸出

進階功能
1. 檔案格式
PyArrow 支持讀取和寫入多種文件格式,如 Parquet 和 Feather。 這些格式經過性能優化並廣泛用於資料處理管道。
2. 記憶體映射
PyArrow 支持記憶體映射文件存取,允許高效地讀寫大資料集而無需將整個資料集載入到記憶體中。
3. 跨進程通信
PyArrow 提供跨進程通信工具,使不同進程之間能高效共享資料。
介紹 IronPDF

IronPDF 是一個用於 Python 的程式庫,方便地處理 PDF 文件,使得程式生成、編輯和操控 PDF 文件變得簡單。 它提供了例如從 HTML 生成 PDF、向已存在的 PDF 新增文字、圖像和形狀,以及從 PDF 文件提取文字和圖像等功能。 以下是一些主要功能:
從 HTML 生成 PDF
IronPDF可以輕鬆將 HTML 文件、HTML 字串和 URL 轉換為 PDF 文件。 利用 Chrome PDF 渲染器將網頁直接渲染成 PDF 格式。
跨平台相容性
IronPDF 與 Python 3+ 相容,並在 Windows、Mac、Linux 和雲端平台上無縫運作。 It is also supported in .NET, Java, Python, and Node.js.
編輯和簽章功能
通過設置屬性、新增安全功能(如密碼和權限)以及應用電子簽名等來增強 PDF 文件。
自定義頁面模板和設置
using IronPDF,您可以通過可自定義的頁眉、頁腳、頁碼和可調節的邊距來定製 PDF。 它支持響應式佈局,允許設置自定義紙張尺寸。
標準合規
IronPDF符合 PDF 標準,包括 PDF/A 和 PDF/UA。 它支持 UTF-8 字元編碼,並無縫處理諸如圖像、CSS 樣式和字體等資產。
使用 IronPDF 和 PyArrow 生成 PDF 文件
IronPDF的先決條件
- IronPDF 使用 .NET 6.0 作為其底層技術。 因此,您需要安裝 .NET 6.0 runtime 在您的系統上。
- Python 3.0+: 您需要安裝Python 3或更新版本。
- pip:安裝 Python 軟體包安裝器 pip 用於 IronPDF 軟體包安裝。
安裝必要的庫:
pip install pyarrow ironpdf
然後新增以下程式碼以展示 IronPDF 與 PyArrow Python 軟體包的使用:
import pandas as pd
import pyarrow as pa
from ironpdf import *
# Apply your license key
License.LicenseKey = "license"
# Create a Pandas DataFrame
df = pd.DataFrame({
'col1': [1, 2, 3],
'col2': ['a', 'b', 'c'],
'col3': [1.1, 2.2, 3.3]
})
# Convert the DataFrame to a PyArrow Table
table = pa.Table.from_pandas(df)
# Display the table
print(table)
#create a PDF renderer
renderer = ChromePdfRenderer()
# Create a PDF from an HTML string using Python
content = "<h1>Awesome Iron PDF with pyarrow</h1>"
content += "<p>table data</p>"
# Iterate over table rows
for row in table:
# Access specific values in a row
value_in_column1 = row[0]
value_in_column2 = row[1]
value_in_column3 = row[2]
# Append row data to content
content += "<p>"+str(value_in_column1)+","+str(value_in_column2)+","+str(value_in_column3)+"</p>"
# Render the HTML content to a PDF
pdf = renderer.RenderHtmlAsPdf(content)
# Export to a file or stream
pdf.SaveAs("DemoPyarrow.pdf")import pandas as pd
import pyarrow as pa
from ironpdf import *
# Apply your license key
License.LicenseKey = "license"
# Create a Pandas DataFrame
df = pd.DataFrame({
'col1': [1, 2, 3],
'col2': ['a', 'b', 'c'],
'col3': [1.1, 2.2, 3.3]
})
# Convert the DataFrame to a PyArrow Table
table = pa.Table.from_pandas(df)
# Display the table
print(table)
#create a PDF renderer
renderer = ChromePdfRenderer()
# Create a PDF from an HTML string using Python
content = "<h1>Awesome Iron PDF with pyarrow</h1>"
content += "<p>table data</p>"
# Iterate over table rows
for row in table:
# Access specific values in a row
value_in_column1 = row[0]
value_in_column2 = row[1]
value_in_column3 = row[2]
# Append row data to content
content += "<p>"+str(value_in_column1)+","+str(value_in_column2)+","+str(value_in_column3)+"</p>"
# Render the HTML content to a PDF
pdf = renderer.RenderHtmlAsPdf(content)
# Export to a file or stream
pdf.SaveAs("DemoPyarrow.pdf")程式碼說明
該腳本演示了將 Pandas、PyArrow 和 IronPDF 程式庫整合在一起,從 Pandas DataFrame 中的資料建立一次 PDF 文件:
Pandas DataFrame 创造:
- 使用三個包含數字和字串資料的列 (
col1,col2,col3) 创造 Pandas DataFrame (df)。
- 使用三個包含數字和字串資料的列 (
轉換爲 PyArrow 表:
- 使用
pa.Table.from_pandas()方法將 Pandas DataFrame (df) 轉換爲 PyArrow 表 (table)。 這種轉換便於高效資料處理和與基於 Arrow 的應用程式的互操作性。
- 使用
使用 IronPDF 生成 PDF:
- 使用 IronPDF 的 ChromePdfRenderer 並調用其 RenderHtmlAsPdf 方法,從包括 PyArrow 表 (
table) 中提取的表頭和資料的 HTML 字串 (content) 生成 PDF 文档 (DemoPyarrow.pdf)。
- 使用 IronPDF 的 ChromePdfRenderer 並調用其 RenderHtmlAsPdf 方法,從包括 PyArrow 表 (
輸出

輸出 PDF

IronPDF 授权
將授權金鑰放在腳本開始處,以便在使用 IronPDF 套件之前使用:
from ironpdf import *
# Apply your license key
License.LicenseKey = "key"from ironpdf import *
# Apply your license key
License.LicenseKey = "key"結論
PyArrow 是一個多功能且強大的程式庫,可增強 Python 在資料處理任務中的功能。 其高效的記憶體格式、互操作性特性和與 Pandas 的整合使其成爲資料科學家和工程師的必備工具。 無論您是在處理大型資料集、執行復雜資料操作,還是構建資料處理管道,PyArrow 都能提供執行這些任務所需的性能和靈活性。 另一方面,IronPDF 是一個強大的 Python 程式庫,簡化了從 Python 應用程式直接建立、操控和渲染 PDF 文件的過程。 它無縫地整合到現有的 Python 框架中,允許開發人員動態生成和自定義 PDF 文件。 結合 PyArrow 和 IronPDF 兩個 Python 軟體包,使用者可以輕鬆處理資料結構並存檔資料。
IronPDF 還提供了全面的文件支持開發人員入門,並附有許多展示其強大功能的程式碼範例。 如需進一步詳情,請存取 文件 和 程式碼範例 頁面。










