Initializing playground…
← Back to roadmap
insertColumn / popColumn
← tsb playground
Example 1 — insertColumn
TypeScript
▶ Run
↺ Reset
import { DataFrame, insertColumn } from "tsb"; const df = DataFrame.fromColumns({ name: ["Alice", "Bob", "Carol"], age: [30, 25, 35], }); // columns: ["name", "age"] // Insert "city" between "name" and "age" const df2 = insertColumn(df, 1, "city", ["NY", "LA", "SF"]); // df2.columns.values → ["name", "city", "age"] // df2.col("city").values → ["NY", "LA", "SF"] // Original is unchanged // df.columns.values → ["name", "age"] console.log(df2);
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Example 2 — Insert with a Series
TypeScript
▶ Run
↺ Reset
import { DataFrame, Series, insertColumn } from "tsb"; const df = DataFrame.fromColumns({ a: [1, 2, 3], b: [4, 5, 6] }); const salary = new Series({ data: [100_000, 90_000, 120_000], name: "salary" }); const df2 = insertColumn(df, 0, "salary", salary); // df2.columns.values → ["salary", "a", "b"] console.log(df2);
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Example 3 — popColumn
TypeScript
▶ Run
↺ Reset
import { DataFrame, popColumn } from "tsb"; const df = DataFrame.fromColumns({ id: [1, 2, 3], name: ["Alice", "Bob", "Carol"], age: [30, 25, 35], }); // Remove "age" and keep the Series const { series: ageSeries, df: df2 } = popColumn(df, "age"); // ageSeries.values → [30, 25, 35] // df2.columns.values → ["id", "name"] // df.columns.values → ["id", "name", "age"] ← original unchanged console.log(df);
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Example 4 — reorderColumns
TypeScript
▶ Run
↺ Reset
import { DataFrame, reorderColumns } from "tsb"; const df = DataFrame.fromColumns({ a: [1], b: [2], c: [3], d: [4] }); // Reverse the column order const df2 = reorderColumns(df, ["d", "c", "b", "a"]); // df2.columns.values → ["d", "c", "b", "a"] // Select a subset (drops columns not listed) const df3 = reorderColumns(df, ["a", "c"]); // df3.columns.values → ["a", "c"] (b and d are dropped) console.log(df3);
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Example 5 — moveColumn
TypeScript
▶ Run
↺ Reset
import { DataFrame, moveColumn } from "tsb"; const df = DataFrame.fromColumns({ year: [2020, 2021, 2022], value: [10, 20, 30], label: ["a", "b", "c"], }); // columns: ["year", "value", "label"] // Move "label" to the front const df2 = moveColumn(df, "label", 0); // df2.columns.values → ["label", "year", "value"] console.log(df2);
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent
Error cases
TypeScript
▶ Run
↺ Reset
// Duplicate column name (default: not allowed) console.log(insertColumn(df, 1, "a", [1, 2, 3])); // → RangeError: Column "a" already exists. Use allowDuplicates=true to permit... // Out-of-range loc console.log(insertColumn(df, 99, "x", [1, 2, 3])); // → RangeError: loc=99 is out of range [0, 2]. // Wrong number of values console.log(insertColumn(df, 0, "x", [1])); // df has 3 rows // → RangeError: values length 1 does not match DataFrame row count 3. // Column not found console.log(popColumn(df, "missing")); // → RangeError: Column "missing" not found in DataFrame.
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent