Initializing playground…
← Back to roadmap

🔀 pivot & pivotTable — Interactive Playground

Reshape DataFrames — mirrors pandas.DataFrame.pivot() and pandas.pivot_table().
Edit any code block below and press ▶ Run (or Ctrl+Enter) to execute it live in your browser.

1 · pivot: basic reshape

pivot reshapes a DataFrame using unique values in one column as the new column headers. Requires one unique value per (row, column) pair.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

2 · pivot: multiple value columns

When values is omitted, all non-index/non-column columns are used. Column names become valCol_colHdr.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

3 · pivotTable: mean aggregation

pivotTable aggregates values when multiple rows map to the same (index, column) cell.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

4 · pivotTable: sum with fill_value

Use aggfunc: "sum" to total up values per cell, and fill_value to replace missing cells.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

5 · pivotTable: count

Use aggfunc: "count" to count how many rows fall into each cell.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

API Reference

pivot requires one unique value per (index, column) pair. pivotTable aggregates when duplicates exist.

// Reshape without aggregation
pivot(df, {
  index:   string,             // column → row labels
  columns: string,             // column → new column headers
  values?: string,             // column → cell values (all remaining if omitted)
}): DataFrame

// Reshape with aggregation
pivotTable(df, {
  index:      string,          // column → row labels
  columns:    string,          // column → new column headers
  values:     string,          // column → cell values
  aggfunc?:   "mean" | "sum" | "count" | "min" | "max",  // default "mean"
  fill_value?: number,         // replace missing cells
}): DataFrame