Skip to main content

Use this package, sparkhistogram, together with PySpark for generating data histograms using the Spark DataFrame API. Currently, the package contains only two functions covering some of the most common and low-complexity use cases.

Use:

  • from sparkhistogram import computeHistogram -> computeHistogram is a function to compute the count/frequency histogram of a given DataFrame column
  • from sparkhistogram import computeWeightedHistogram -> computeWeightedHistogram is a function to compute the weighted histogram of a given DataFrame column
def computeHistogram(df: "DataFrame", value_col: str, min_val: float, max_val: float, bins: int) -> "DataFrame"

Parameters
----------
df: the dataframe with the data to compute
value_col: column name on which to compute the histogram
min_val: minimum value in the histogram
max_val: maximum value in the histogram
bins: number of histogram buckets to compute

Output DataFrame
----------------
bucket: the bucket number, range from 1 to bins (included)
value: midpoint value of the given bucket
count: number of values in the bucket

Run this example in the PySpark shell

Note: requires PySpark version 3.1 or higher.

bin/pyspark

# import the helper function to generate the histogram using Spark DataFrame operations
from sparkhistogram import computeHistogram

# generate some toy data
num_events = 100
scale = 100
seed = 4242
df = spark.sql(f"select random({seed}) * {scale} as random_value from range({num_events})")

# define the DataFrame transformation to compute the histogram 
histogram = computeHistogram(df, "random_value", -20, 90, 11) 

# with Spark 3.3.0 and higher you can also use df.transform
# histogram = df.transform(computeHistogram, "random_value", -20, 90, 11)

# fetch and display the (toy) data
histogram.show()

# expected output:
+------+-----+-----+
|bucket|value|count|
+------+-----+-----+
|     1|-15.0|    0|
|     2| -5.0|    0|
|     3|  5.0|    6|
|     4| 15.0|   10|
|     5| 25.0|   15|
|     6| 35.0|   12|
|     7| 45.0|    9|
|     8| 55.0|    7|
|     9| 65.0|   10|
|    10| 75.0|   16|
|    11| 85.0|    7|
+------+-----+-----+

More details and notebooks with matplotlib visualization of the histograms at:
https://github.com/LucaCanali/Miscellaneous/blob/master/Spark_Notes/Spark_DataFrame_Histograms.md

Metadata

Release files for sparkhistogram 0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sparkhistogram 0.4
File Size Uploaded Provenance Actions
sparkhistogram-0.4.tar.gz 3.2 kB Without provenance Details for sparkhistogram-0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sparkhistogram 0.4
File Interpreter ABI Platform Actions
sparkhistogram-0.4-py2.py3-none-any.whl Python 3, Python 2 none any Details for sparkhistogram-0.4-py2.py3-none-any.whl

Total release size: 6.9 kB

Release files / sparkhistogram-0.4.tar.gz

Download URL sparkhistogram-0.4.tar.gz
Size 3.2 kB
Tags Source
SHA-256 checksum
How to use checksums
022338b978f0cea6bde2933c24122328fcc8214c13422fe919d0b467e7e75fe1
BLAKE2b-256 checksum
How to use checksums
790f30a59ff6c9d2175ada826b6ad3c095bedc394335c3c839aa858d54bdd711
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Release files / sparkhistogram-0.4-py2.py3-none-any.whl

Download URL sparkhistogram-0.4-py2.py3-none-any.whl
Size 3.7 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
aad590cba577a0fa83781c14446e245e929a4b6578ffac12f347750684615202
BLAKE2b-256 checksum
How to use checksums
b6a1e8032f77bd96c72c63ecdd7666a10bbb440ba932b9c6be7d18984081a2ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.11.7

Security for sparkhistogram 0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page