A utility library for modifying PySpark DataFrames.
Project description
pyspark-data-prep
A simple and robust utility package for modifying PySpark DataFrames.
pyspark-data-prep encapsulates common PySpark DataFrame operations, such as adding new rows, into a clean and reusable class. This makes your data processing code more modular, readable, and easier to maintain.
Features
- Encapsulation: The
DataFrameModifierclass provides a single, organized interface for common DataFrame operations. - Robustness: The
add_rowsmethod usesunionByName(), which handles schema alignment by column name, ensuring consistency and preventing errors. - Non-Destructive: All methods return a new DataFrame, adhering to the immutability principle of PySpark.
- Scalable: Built on PySpark's core principles, the package is designed to handle large datasets efficiently.
Installation
You can install this package directly from PyPI using pip.
pip install pyspark-data-prep
Usage
The primary component of this package is the DataFrameModifier class. Here is a complete example demonstrating how to use the add_rows method to append new data to an existing DataFrame.
from pyspark.sql import SparkSession
from pyspark.sql.types import StructType, StructField, StringType, IntegerType
from pyspark_data_prep.df_modifier import DataFrameModifier
# 1. Initialize Spark Session
spark = SparkSession.builder.appName("DataFrameModifier Example").getOrCreate()
# 2. Define schema and create an initial DataFrame
schema = StructType([
StructField("name", StringType(), True),
StructField("age", IntegerType(), True),
])
initial_data = [("Alice", 25), ("Bob", 30)]
initial_df = spark.createDataFrame(data=initial_data, schema=schema)
print("--- Original DataFrame ---")
initial_df.show()
initial_df.printSchema()
# 3. Instantiate the modifier class
modifier = DataFrameModifier(spark)
# 4. Data for the new rows (a list of tuples)
new_rows_data = [("Charlie", 35), ("Dana", 40)]
# 5. Add the new rows using the class method
modified_df = modifier.add_rows(initial_df, new_rows_data)
print("\n--- New DataFrame after adding rows ---")
modified_df.show()
modified_df.printSchema()
# 6. Stop the SparkSession when done
spark.stop()
API Reference
DataFrameModifier Class
__init__(self, spark_session)
Initializes the DataFrameModifier with a Spark session.
- Parameters:
spark_session- An active SparkSession instance
add_rows(self, df, new_rows_data)
Adds new rows to an existing DataFrame.
- Parameters:
df- The original PySpark DataFramenew_rows_data- List of tuples containing data for new rows
- Returns: A new DataFrame with the added rows
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
If you encounter any issues or have questions, please open an issue on the GitHub repository.
Note: Remember that for import statements, use underscores (pyspark_data_prep) instead of hyphens, as Python doesn't support hyphens in import names.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyspark_data_prep-1.0.0.tar.gz.
File metadata
- Download URL: pyspark_data_prep-1.0.0.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01e16fa12f09037d1cdacab6cb648fad244f70abec3b013f2a003fc695e57100
|
|
| MD5 |
4a8362f4dd2a34249eaa706030d01861
|
|
| BLAKE2b-256 |
25257154cb5e77119b5873d2ba5fbb6fe3a0394315fd00888deba03cf5e4b299
|
Provenance
The following attestation bundles were made for pyspark_data_prep-1.0.0.tar.gz:
Publisher:
python-publish.yml on githubLINGESH/pyspark_data_prep
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyspark_data_prep-1.0.0.tar.gz -
Subject digest:
01e16fa12f09037d1cdacab6cb648fad244f70abec3b013f2a003fc695e57100 - Sigstore transparency entry: 533419173
- Sigstore integration time:
-
Permalink:
githubLINGESH/pyspark_data_prep@af2e95e88b16971eb6c542a05bed330df716cc7e -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/githubLINGESH
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@af2e95e88b16971eb6c542a05bed330df716cc7e -
Trigger Event:
release
-
Statement type:
File details
Details for the file pyspark_data_prep-1.0.0-py3-none-any.whl.
File metadata
- Download URL: pyspark_data_prep-1.0.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f516c29183854b1177538e82589875da1939c30e33df720253a270a418f7a2c3
|
|
| MD5 |
9eb2738bc03aa9cf8d372351dfba6fb2
|
|
| BLAKE2b-256 |
ae92be06b9670c24719a42f5e7170a273c8ef5effc0269c941ab903ee53fe3bf
|
Provenance
The following attestation bundles were made for pyspark_data_prep-1.0.0-py3-none-any.whl:
Publisher:
python-publish.yml on githubLINGESH/pyspark_data_prep
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyspark_data_prep-1.0.0-py3-none-any.whl -
Subject digest:
f516c29183854b1177538e82589875da1939c30e33df720253a270a418f7a2c3 - Sigstore transparency entry: 533419177
- Sigstore integration time:
-
Permalink:
githubLINGESH/pyspark_data_prep@af2e95e88b16971eb6c542a05bed330df716cc7e -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/githubLINGESH
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@af2e95e88b16971eb6c542a05bed330df716cc7e -
Trigger Event:
release
-
Statement type: