xs — Cross-Section Selection

xsDataFrame(df, key) extracts a row by label as a Series, or a column by name (with axis: 1). Works with both flat and MultiIndex DataFrames.

Interactive Demo

Click a button above to run an example.

Code Examples

import { DataFrame, xsDataFrame, xsSeries, MultiIndex } from "tsb";

// ── flat index ──────────────────────────────────────────────────────────────
const df = DataFrame.fromColumns(
  { a: [1, 2, 3], b: [4, 5, 6] },
  { index: ["x", "y", "z"] },
);

// Select row "y" → Series { a: 2, b: 5 }
xsDataFrame(df, "y");

// Select column "b" → Series { x: 4, y: 5, z: 6 }
xsDataFrame(df, "b", { axis: 1 });

// ── MultiIndex ─────────────────────────────────────────────────────────────
const mi = MultiIndex.fromTuples([
  ["A", 1], ["A", 2],
  ["B", 1], ["B", 2],
]);
const miDf = new DataFrame( ... , mi);

// All "A" rows → DataFrame with 2 rows
xsDataFrame(miDf, "A");

// ── Series ─────────────────────────────────────────────────────────────────
const s = new Series({ data: [10, 20, 30], index: ["a", "b", "c"] });
xsSeries(s, "b"); // → 20