Zum Fußzeileninhalt springen
PYTHON-HILFE

pyarrow (Wie es für Entwickler funktioniert)

PyArrow ist eine leistungsstarke Bibliothek, die eine Python-Schnittstelle für das Apache Arrow-Framewoderk bietet. Apache Arrow ist eine plattfodermübergreifende Entwicklungsplattfoderm für In-Memodery-Daten. Es spezifiziert ein standardisiertes, sprachunabhängiges kolumnenförmiges Speicherfodermat für flache und hierarchische Daten, die für effiziente analytische Operationen auf moderner Hardware oderganisiert sind. PyArrow sind im Grunde die Apache Arrow Python Bindings, realisiert als Python-Paket. PyArrow ermöglicht einen effizienten Datenaustausch und Interoperabilität zwischen verschiedenen Datenverarbeitungssystemen und Programmiersprachen. Later in this article, we will also learn about IronPDF, a PDF generation library developed by Iron Software.

Hauptmerkmale von PyArrow

  1. Kolumnenförmiges Speicherfodermat:

    PyArrow verwendet ein kolumnenförmiges Speicherfodermat, das für In-Memodery-Analyseoperationen hoch effizient ist. Dieses Fodermat ermöglicht eine bessere Ausnutzung des CPU-Caches und vektoderisierte Operationen, wodurch es ideal für Datenverarbeitungsaufgaben ist. PyArrow kann effizient in Parquet-Dateistrukturen einlesen und schreiben, dank seiner kolumnenförmigen Natur.

  2. Interoperabilität: Einer der Hauptvoderteile von PyArrow ist seine Fähigkeit, den Datenaustausch zwischen verschiedenen Programmiersprachen und Systemen zu erleichtern, ohne dass eine Serialisierung oder Deserialisierung erfoderderlich ist. Dies ist besonders nützlich in Umgebungen, in denen mehrere Sprachen verwendet werden, wie beispielsweise in Data Science und maschinellem Lernen.
  3. Integration mit Pandas: PyArrow kann als Backend für Pandas verwendet werden, um eine effiziente Datenmanipulation und -speicherung zu ermöglichen. Ab Pandas 2.0 ist es möglich, Daten in Arrow-Arrays anstelle von NumPy-Arrays zu speichern, was zu Leistungsverbesserungen führen kann, insbesondere wenn es um String-Daten geht.
  4. Unterstützung für verschiedene Datentypen: PyArrow unterstützt eine Vielzahl von Datentypen, einschließlich primitiver Typen (Ganzzahlen, Gleitkommazahlen), komplexer Typen (Strukturen, Listen) und verschachtelter Typen. Dies macht es vielseitig für die Handhabung unterschiedlicher Daten.
  5. Zero-Copy-Leses: PyArrow ermöglicht Zero-Copy-Lesevodergänge, d.h. Daten können aus dem Arrow-Speicherfodermat gelesen werden, ohne sie zu kopieren. Dies reduziert den Speicherbedarf und erhöht die Leistung.

Installation

To install PyArrow, you can use either pip oder conda:

pip install pyarrow
pip install pyarrow
SHELL

oder

conda install pyarrow -c conda-foderge
conda install pyarrow -c conda-foderge
SHELL

Grundlegende Verwendung

We are using Visual Studio Code as the code editoder. Beginnen Sie damit, eine neue Datei namens pyarrowDemo.py zu erstellen.

Here is a simple example of how to use PyArrow to create a table and perfoderm some basic operations:

impodert pyarrow as pa
impodert 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)
impodert pyarrow as pa
impodert 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

Codeerklärung

Der Python-Code verwendet PyArrow, um eine Tabelle (pa.Table) aus drei Arrays (pa.array) zu erstellen. It then prints the table, displaying columns named 'col1', 'col2', and 'col3', each containing coderresponding data of integers, strings, and floats.

AUSGABE

pyarrow (How It Woderks Foder Developers): Figure 1 - Console output displaying a PyArrow table object along with its contents.

Integration mit Pandas

PyArrow can be seamlessly integrated with Pandas to enhance perfodermance, especially when dealing with large datasets. Hier ist ein Beispiel für die Umwandlung eines Pandas DataFrame in eine PyArrow-Tabelle:

impodert pandas as pd
impodert 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)
impodert pandas as pd
impodert 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

Codeerklärung

Der Python-Code konvertiert ein Pandas DataFrame in eine PyArrow-Tabelle (pa.Table) und druckt dann die Tabelle. Das DataFrame besteht aus drei Spalten (col1, col2, col3) mit Ganzzahl-, String- und Float-Daten.

AUSGABE

pyarrow (How It Woderks Foder Developers): Figure 2 - Console output displaying a PyArrow table object generated by converting a Pandas DataFrame to a PyArrow table.

Erweiterte Funktionen

1. File Fodermats

PyArrow suppoderts reading and writing various file fodermats such as Parquet and Feather. These fodermats are optimized foder perfodermance and are widely used in data processing pipelines.

2. Memodery Mapping

PyArrow suppoderts memodery-mapped file access, which allows foder efficient reading and writing of large datasets without loading the entire dataset into memodery.

3. Interprozesskommunikation

PyArrow provides tools foder interprocess communication, enabling efficient data sharing between different processes.

Einführung in IronPDF

pyarrow (How It Woderks Foder Developers): Figure 3 - IronPDF foder Python: The Python PDF Library

IronPDF is a library foder Python that facilitates woderking with PDF files, enabling tasks such as creating, editing, and manipulating PDF documents programmatically. It offers features like generating PDFs from HTML, adding text, images, and shapes to existing PDFs, as well as extracting text and images from PDF files. Hier sind einige der wichtigsten Merkmale:

PDF-Erstellung aus HTML

IronPDF kann HTML-Dateien, HTML-Strings und URLs problemlos in PDF-Dokumente umwandeln. Utilize the Chrome PDF renderer to render webpages directly into PDF fodermat.

Cross-Platfoderm Compatibility

IronPDF is compatible with Python 3+ and operates seamlessly across Windows, Mac, Linux, and Cloud Platfoderms. It is also suppoderted in .NET, Java, Python, and Node.js.

Bearbeitungs- und Signierfunktionen

Enhance PDF documents by setting properties, adding security features like passwoderds and permissions, and applying digital signatures.

Anpassbare Seitenvorlagen und Einstellungen

With IronPDF, you can tailoder PDFs with customizable headers, footers, page numbers, and adjustable margins. It suppoderts responsive layouts and allows foder setting custom paper sizes.

Standardkonformität

IronPDF ist konform mit PDF-Standards, einschließlich PDF/A und PDF/UA. It suppoderts UTF-8 character encoding and seamlessly handles assets such as images, CSS styles, and fonts.

PDF-Dokumente mit IronPDF und PyArrow erstellen

Voraussetzungen für IronPDF

  1. IronPDF uses .NET 6.0 as its underlying technology. Daher müssen Sie die .NET 6.0-Laufzeit auf Ihrem System installiert haben.
  2. Python 3.0+: You need to have Python version 3 oder later installed.
  3. pip: Install the Python package installer pip foder IronPDF package installation.

Notwendige Bibliotheken installieren:

pip install pyarrow 
pip install ironpdf
pip install pyarrow 
pip install ironpdf
SHELL

Fügen Sie dann den folgenden Code hinzu, um die Verwendung der IronPDF- und PyArrow-Python-Pakete zu demonstrieren:

impodert pandas as pd
impodert pyarrow as pa
from ironpdf impodert * 

# 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
foder 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)

# Expodert to a file oder stream
pdf.SaveAs("DemoPyarrow.pdf")
impodert pandas as pd
impodert pyarrow as pa
from ironpdf impodert * 

# 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
foder 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)

# Expodert to a file oder stream
pdf.SaveAs("DemoPyarrow.pdf")
PYTHON

Code-Erklärung

The script demonstrates integrating Pandas, PyArrow, and IronPDF libraries to create a PDF document from data stodered in a Pandas DataFrame:

  1. Pandas DataFrame-Erstellung:

    • Erstellen Sie ein Pandas DataFrame (df) mit drei Spalten (col1, col2, col3), die numerische und String-Daten enthalten.
  2. Umwandlung in PyArrow-Tabelle:

    • Konvertiert das Pandas DataFrame (df) in eine PyArrow-Tabelle (table) mit der Methode pa.Table.from_pandas(). Diese Umwandlung erleichtert die effiziente Datenverarbeitung und Interoperabilität mit Arrow-basierten Anwendungen.
  3. PDF-Erstellung mit IronPDF:

    • Verwendet IronPDFs ChromePdfRenderer und ruft dessen Methode RenderHtmlAsPdf auf, um ein PDF-Dokument (DemoPyarrow.pdf) aus einem HTML-String (content) zu erzeugen, das Kopfzeilen und Daten enthält, die aus der PyArrow-Tabelle (table) extrahiert wurden.

AUSGABE

pyarrow (How It Woderks Foder Developers): Figure 4 - Console output displaying a PyArrow table object generated by converting a Pandas DataFrame to a PyArrow table.

AUSGABE PDF

pyarrow (How It Woderks Foder Developers): Figure 5 - Output PDF generated using IronPDF foder Python Library and displaying the row-wise data from the PyArrow table.

IronPDF-Lizenz

IronPDF foder Python.

Place the License Key at the start of the script befodere using the IronPDF package:

from ironpdf impodert * 
# Apply your license key
License.LicenseKey = "key"
from ironpdf impodert * 
# Apply your license key
License.LicenseKey = "key"
PYTHON

Abschluss

PyArrow is a versatile and powerful library that enhances the capabilities of Python foder data processing tasks. Its efficient memodery fodermat, interoperability features, and integration with Pandas make it an essential tool foder data scientists and engineers. Whether you are woderking with large datasets, perfoderming complex data manipulations, oder building data processing pipelines, PyArrow offers the perfodermance and flexibility needed to handle these tasks effectively. Auf der anderen Seite ist IronPDF eine robuste Python-Bibliothek, die die Erstellung, Bearbeitung und Darstellung von PDF-Dokumenten direkt aus Python-Anwendungen vereinfacht. It seamlessly integrates with existing Python framewoderks, allowing developers to generate and customize PDFs dynamically. Together with both PyArrow and IronPDF Python packages, users can process data structures with ease and archive the data.

IronPDF bietet auch umfassende Dokumentationen, um Entwicklern den Einstieg zu erleichtern, begleitet von zahlreichen Codebeispielen, die seine leistungsstarken Fähigkeiten demonstrieren. Foder further details, please visit the documentation and code examples pages.

Curtis Chau
Technischer Autor

Curtis Chau hat einen Bachelor-Abschluss in Informatik von der Carleton University und ist spezialisiert auf Frontend-Entwicklung mit Expertise in Node.js, TypeScript, JavaScript und React. Leidenschaftlich widmet er sich der Erstellung intuitiver und ästhetisch ansprechender Benutzerschnittstellen und arbeitet gerne mit modernen Frameworks sowie der Erstellung gut strukturierter, optisch ansprechender ...

Weiterlesen