# Collapse CSV rows into one cell per group

Group CSV rows by key columns and join another column into one delimited cell, with dedupe, sorting, and browser-local processing.

## Run it

- **CLI:** `gizza tool csv-collapse-rows "region,product
East,Apple
West,Banana
East,Cherry
East,Apple" 'key_columns=region' 'collapse_column=product'`
- **Web:** https://gizza.ai/tools/csv-collapse-rows/
- **Agents:** machine-readable descriptor (parameters JSON Schema) at https://gizza.ai/tools/csv-collapse-rows/tool.json

## Inputs

- `data` — CSV data _(field)_
- `key_columns` — Group-by column(s) _(field)_
- `collapse_column` — Column to collapse _(field)_
- `separator` — Value separator _(field)_
- `dedupe` — Dedupe values within each group _(field)_
- `skip_empty` — Skip blank cells in the collapse column _(field)_
- `sort_values` — Sort collapsed values _(field)_
- `delimiter` — Field delimiter _(field)_
- `has_header` — First row is a header _(field)_

## Output

- Collapsed CSV (text)

## Query parameters

Open the tool pre-filled and auto-run via URL:

- `data` — CSV data
- `key_columns` — Group-by column(s)
- `collapse_column` — Column to collapse
- `separator` — Value separator
- `dedupe` — Dedupe values within each group
- `skip_empty` — Skip blank cells in the collapse column
- `sort_values` — Sort collapsed values
- `delimiter` — Field delimiter
- `has_header` — First row is a header

Example: `https://gizza.ai/tools/csv-collapse-rows/?data=region%2Cproduct%0AEast%2CApple%0AWest%2CBanana%0AEast%2CCherry%0AEast%2CApple&key_columns=region&collapse_column=product&separator=%2C%20&dedupe=true&skip_empty=true&sort_values=none&delimiter=comma&has_header=true`

---

## About this tool

Collapse rows groups a CSV by one or more **key columns** and joins another
column's values from every row in the group into a single delimited cell. It is
the spreadsheet-and-file equivalent of SQL `GROUP_CONCAT` / `STRING_AGG` /
`LISTAGG`, pandas `groupby(keys)[col].agg(", ".join)`, and R dplyr
`summarise(paste(x, collapse = ", "))`.

Point it at the columns to group by and the column to collapse, and it returns
**one row per group** — the key columns followed by the collapsed column — in
first-seen order. You can dedupe repeated values (like `DISTINCT`), sort them
ascending or descending, skip blank cells, choose the separator between values,
and switch the field delimiter to tab, semicolon, or pipe. Everything runs
locally in your browser; the CSV never leaves your machine.

### Worked example

Input:

```
region,product
East,Apple
West,Banana
East,Cherry
East,Apple
```

With **group-by** `region`, **collapse** `product`, and **dedupe** on:

```
region,product
East,"Apple, Cherry"
West,Banana
```

The two `East` rows merge into one; the repeated `Apple` is dropped by dedupe,
and the cell is quoted because it contains the comma separator.

## Limits & edge cases

- Columns can be referenced by **header name** or **1-based index** (`1` = first
  column). Turn off *First row is a header* to treat every row as data — then use
  indices only, and output columns are labelled `col_1`, `col_2`, ….
- Output keeps only the **key columns and the collapse column**; any other
  columns are dropped.
- Groups and values within a cell are emitted in **first-seen order** unless you
  choose a sort. Sorting is lexicographic (text), so `10` sorts before `2`.
- **Skip blank cells** (on by default) drops empty values before joining; turn it
  off to keep empty positions (e.g. `a,,b`).
- The value separator can be any string (default `, `). The field delimiter is
  separate and applies to both input parsing and output.

## FAQ

<details>
<summary>How is this different from a group-by that sums or counts?</summary>

This tool concatenates the text values of one column into a list; it does not
compute numeric aggregates like sum, average, or count. If you need those, use a
CSV group-by / aggregate tool instead. Collapse rows is for turning many rows of
labels, tags, or IDs into one comma-separated cell per group.

</details>

<details>
<summary>Can I group by more than one column?</summary>

Yes. Enter a comma-separated list in **Group-by column(s)** — for example
`year,team`. Rows are grouped by the exact combination of those key values, and
both key columns are carried through to the output.

</details>

<details>
<summary>Why is a collapsed cell wrapped in double quotes?</summary>

Standard CSV quoting. When the joined value contains the field delimiter (a comma
by default), a line break, or a quote, the cell is wrapped in double quotes so the
result re-parses correctly. Choosing a different separator or field delimiter can
avoid the quoting if you prefer.

</details>

<details>
<summary>Does the order of the collapsed values match my input?</summary>

By default, yes — values appear in the order they were first seen, matching SQL
`GROUP_CONCAT` without an `ORDER BY`. Choose **Ascending** or **Descending** under
*Sort collapsed values* to order them alphabetically instead. Dedupe also keeps
the first occurrence of each value.

</details>

<details>
<summary>My file has no header row — can I still use it?</summary>

Yes. Turn off **First row is a header**. Then every row is treated as data, you
reference columns by 1-based index (`1`, `2`, …), and the output gets generated
column labels `col_1`, `col_2`, … so the result is still a valid CSV.

</details>

## Related tools

- [CSV Group By](https://gizza.ai/tools/csv-group-by/): Group CSV rows by a column and aggregate others (count, sum, average, min, max) — in your browser. Free, private, no upload.
- [Amazon Order Analyzer](https://gizza.ai/tools/amazon-order-analyzer/): Paste an Amazon order-history CSV export to summarize total spend by month, top items, and category breakdowns. Browser-only, private, with Markdown or JSON output.
- [Avro to JSON Converter](https://gizza.ai/tools/avro-to-json/): Decode Apache Avro Object Container Files (.avro / OCF) to JSON, NDJSON, or the embedded schema — no .avsc needed, free and private in your browser.
- [Reconcile bank statement and ledger CSVs](https://gizza.ai/tools/bank-statement-reconcile/): Match bank-statement CSV rows to ledger rows by date, signed amount, and fuzzy memo similarity, with unmatched and suggested matches.
- [Bencode decoder](https://gizza.ai/tools/bencode-decoder/): Decode bencode (the BitTorrent / .torrent serialization format) into readable JSON, and re-encode JSON back into canonical bencode — in your browser. Nothing is uploaded.
