1---2name: write-dataframe-transformation3description: Writes pandas or polars code for a described transformation with built-in checks on row counts, nulls, key uniqueness and join cardinality. Use when reshaping, joining or aggregating data.4license: CC0-1.05arguments:6 - input_description7 - desired_output8 - library9argument-hint: <input_description> <desired_output> [library]10disable-model-invocation: true11metadata:12 version: 1.1.013 kind: prompt14 category: data-exploration15 source: https://skillmd.com/skills/hermes-hq/write-dataframe-transformation16 upstream: https://hermes-ide.com/prompts/write-dataframe-transformation17 catalog: 2026.1014.118---1920# Write a dataframe transformation2122## Inputs2324- `input_description` (required): The input tables, with column names, types, keys and a few sample rows if you can share them.25- `desired_output` (required): What the result should look like (grain, columns, filters, aggregations), ideally with an example row.26- `library` (optional; one of: pandas, polars; default: pandas): The dataframe library to use.2728Arguments fill these in order. If a required value is empty, take it from the user’s message or ask for it once.2930<context>31Dataframe code usually fails silently, not loudly: a join on a key that is not unique multiplies rows, a left join leaves nulls that later vanish in an aggregation, a string key with trailing spaces matches nothing, and dates parsed in the wrong format shift by months. The output looks plausible and is wrong. Defensive transformations state the grain of every table, check keys before joining, assert row counts and nulls at each step, and fail with a clear message instead of producing a wrong table.32</context>3334<task>35Write $library code that turns this input:36<input>37$input_description38</input>39into this output:40<output>41$desired_output42</output>43441. State the grain (what one row represents) and the key of each input and of the output.452. Plan the steps in order: load or receive, clean types and keys, filter, join, reshape, aggregate, final selection and ordering.463. Write the code as a function that takes the input dataframes and returns the output, with a short comment on each step.474. After each step that can change row counts or introduce nulls, add a check:48 - keys: uniqueness on the side that should be unique, before every join;49 - joins: the expected cardinality (pandas `merge(..., validate="many_to_one")`, polars `join(..., validate="m:1")`) and a count of unmatched keys;50 - row counts: expected equal, smaller or larger than before, and by how much;51 - nulls: in key columns and in columns the output requires;52 - aggregates: totals that should be preserved (for example the sum of amounts before and after reshaping).535. Make checks raise an error with a message that names the step and the offending values; do not use bare `assert`, which `python -O` removes.546. Add a tiny test: a few hand-made input rows, including one edge case (duplicate key, missing value or unmatched join), and the exact expected output.55</task>5657<constraints>58- Use idiomatic, vectorised $library: for pandas, method chaining where it stays readable, `.loc` for assignment, no chained assignment and no row-wise `apply` when a vectorised form exists; for polars, expressions with `pl.col`, and the lazy API for large data.59- Write code compatible with current stable releases, and name any feature that needs a recent version.60- Do not guess column names, types or business rules. If the description does not give the columns and keys of each input, or the grain of the output, stop and ask for exactly those, with a one-line example of the detail you need; do not write code against invented columns.61- For smaller gaps (for example which duplicate to keep, or how to treat unmatched rows), choose the safest behaviour, list it under Assumptions, and make it easy to change.62- Keep it self-contained: imports at the top, no reading from paths you invented; take dataframes as parameters.63</constraints>6465<output_format>66## Assumptions67Bullets: grains, keys and every assumption made.68## Code69One fenced Python block with the function and its checks.70## What the checks catch71A table: check | step | the failure it prevents.72## Test73A fenced Python block with the small test and its expected output.74</output_format>