|
| 1 | +# Python data loader to generate Apache Parquet |
| 2 | + |
| 3 | +Here’s a Python data loader that accesses records for over 91,000 dams from the [National Inventory of Dams](https://nid.sec.usace.army.mil/), limits the data to only four columns, then outputs an Apache Parquet file to standard out. |
| 4 | + |
| 5 | +```python |
| 6 | +# Load libraries (must be installed in environment) |
| 7 | +import pandas as pd |
| 8 | +import pyarrow as pa |
| 9 | +import pyarrow.parquet as pq |
| 10 | +import sys |
| 11 | + |
| 12 | +df = pd.read_csv("https://nid.sec.usace.army.mil/api/nation/csv", low_memory=False, skiprows=1).loc[:, ["Dam Name", "Primary Purpose", "Primary Dam Type", "Hazard Potential Classification"]] |
| 13 | + |
| 14 | +# Write DataFrame to a temporary file-like object |
| 15 | +buf = pa.BufferOutputStream() |
| 16 | +table = pa.Table.from_pandas(df) |
| 17 | +pq.write_table(table, buf, compression="snappy") |
| 18 | + |
| 19 | +# Get the buffer as a bytes object |
| 20 | +buf_bytes = buf.getvalue().to_pybytes() |
| 21 | + |
| 22 | +# Write the bytes to standard output |
| 23 | +sys.stdout.buffer.write(buf_bytes) |
| 24 | +``` |
| 25 | + |
| 26 | +<div class="note"> |
| 27 | + |
| 28 | +To run this data loader you’ll need python3, and the `pandas` and `pyarrow` libraries installed and available on your `$PATH`. |
| 29 | + |
| 30 | +</div> |
| 31 | + |
| 32 | +<div class="tip"> |
| 33 | + |
| 34 | +We recommend using a [Python virtual environment](https://observablehq.com/framework/loaders#venv), such as with venv or uv, and managing required packages via `requirements.txt` rather than installing them globally. |
| 35 | + |
| 36 | +</div> |
| 37 | + |
| 38 | +This example uses the default Snappy compression algorithm. See other [options for compression](https://parquet.apache.org/docs/file-format/data-pages/compression/) available in pyarrow’s [`write_table()`](https://arrow.apache.org/docs/python/generated/pyarrow.parquet.write_table.html) function. |
| 39 | + |
| 40 | +The above data loader lives in `data/us-dams.parquet.py`, so we can load the data using `data/us-dams.parquet`. The `FileAttachment.parquet` method parses the file and returns a promise to an Apache Arrow table. |
| 41 | + |
| 42 | +```js echo |
| 43 | +const dams = FileAttachment("data/us-dams.parquet").parquet(); |
| 44 | +``` |
| 45 | + |
| 46 | +We can display the table using `Inputs.table`. |
| 47 | + |
| 48 | +```js echo |
| 49 | +Inputs.table(dams) |
| 50 | +``` |
| 51 | + |
| 52 | +Lastly, we can pass the table to Observable Plot to make a simple bar chart of dam counts by purpose, with color mapped to hazard classification. |
| 53 | + |
| 54 | +```js echo |
| 55 | + Plot.plot({ |
| 56 | + marginLeft: 220, |
| 57 | + color: {legend: true, domain: ["Undetermined", "Low", "Significant", "High"]}, |
| 58 | + marks: [ |
| 59 | + Plot.barX(dams, |
| 60 | + Plot.groupY( |
| 61 | + {x: "count"}, |
| 62 | + { |
| 63 | + y: "Primary Purpose", |
| 64 | + fill: "Hazard Potential Classification", |
| 65 | + sort: {y: "x", reverse: true} |
| 66 | + } |
| 67 | + ) |
| 68 | + ) |
| 69 | + ] |
| 70 | + }) |
| 71 | +``` |
0 commit comments