+ DataFrame is the heart of tsb: a two-dimensional, column-oriented
+ table where every column is a typed Series. It mirrors pandas.DataFrame
+ with a fully strict TypeScript API.
+
Construction
+Three factory methods cover the most common shapes of input data.
+ +import { DataFrame } from "tsb";
+
+const df = DataFrame.fromColumns({
+ name: ["Alice", "Bob", "Carol"],
+ age: [30, 25, 35],
+ score: [90, 80, 95],
+});
+
+console.log(df.toString());
+ const df = DataFrame.fromRecords([
+ { city: "London", pop: 9_000_000 },
+ { city: "Tokyo", pop: 14_000_000 },
+ { city: "NYC", pop: 8_300_000 },
+]);
+
+df.shape; // [3, 2]
+ const df = DataFrame.from2D(
+ [[1, 4], [2, 5], [3, 6]],
+ ["a", "b"],
+);
+
+df.col("a").values; // [1, 2, 3]
+ Shape & Properties
+ +df.shape; // [3, 3] β [rows, cols]
+df.ndim; // 2
+df.size; // 9 β rows Γ cols
+df.empty; // false
+
+df.index; // RangeIndex(0, 1, 2)
+df.columns; // Index(["name", "age", "score"])
+ Column Access
+ +df.col("age"); // Series β throws if missing
+df.get("age"); // Series | undefined
+df.has("age"); // true
+df.col("age").mean(); // 30
+ Slicing
+ +df.head(2); // first 2 rows
+df.tail(1); // last row
+df.iloc([0, 2]); // rows at positions 0 and 2
+df.loc(["Alice"]); // rows where index label = "Alice"
+ Column Mutations
+// Add or replace a column
+const df2 = df.assign({ bonus: [5, 10, 15] });
+
+// Remove columns
+const df3 = df.drop(["score"]);
+
+// Reorder / subset columns
+const df4 = df.select(["age", "name"]);
+
+// Rename a column
+const df5 = df.rename({ age: "years" });
+ Missing Values
+ +const df = DataFrame.fromColumns({
+ a: [1, null, 3],
+ b: [4, 5, null],
+});
+
+df.isna().toRecords(); // [{ a: false, b: false }, ...]
+df.dropna().shape; // [1, 2] β only row 0 has no nulls
+df.fillna(0).toRecords(); // nulls β 0
+ Aggregations
+ +const df = DataFrame.fromColumns({
+ a: [10, 20, 30],
+ b: [1, 2, 3],
+});
+
+df.sum(); // Series { a: 60, b: 6 }
+df.mean(); // Series { a: 20, b: 2 }
+df.min(); // Series { a: 10, b: 1 }
+df.max(); // Series { a: 30, b: 3 }
+
+df.describe();
+// DataFrame with rows: count, mean, std, min, max
+// columns: a, b
+ Sorting
+ +const df = DataFrame.fromColumns({
+ name: ["Alice", "Bob", "Carol"],
+ score: [90, 80, 95],
+});
+
+// Sort ascending
+df.sortValues("score").col("name").values;
+// ["Bob", "Alice", "Carol"]
+
+// Sort descending
+df.sortValues("score", false).col("name").values;
+// ["Carol", "Alice", "Bob"]
+
+// Multi-column sort
+df.sortValues(["group", "score"], [true, false]);
+ Apply
+ +const df = DataFrame.fromColumns({ a: [1, 2, 3], b: [4, 5, 6] });
+
+// axis=0: apply to each column
+df.apply((s) => s.sum(), 0);
+// Series { a: 6, b: 15 }
+
+// axis=1: apply to each row
+df.apply((s) => s.sum(), 1);
+// Series [5, 7, 9]
+ Iteration
+ +// Iterate over (columnName, Series) pairs
+for (const [name, col] of df.items()) {
+ console.log(name, col.sum());
+}
+
+// Iterate over (rowLabel, rowSeries) pairs
+for (const [label, row] of df.iterrows()) {
+ console.log(label, row.toObject());
+}
+ Conversion
+ +const df = DataFrame.fromColumns({ a: [1, 2], b: [3, 4] });
+
+df.toRecords();
+// [{ a: 1, b: 3 }, { a: 2, b: 4 }]
+
+df.toDict();
+// { a: [1, 2], b: [3, 4] }
+
+df.toArray();
+// [[1, 3], [2, 4]]
+ Index Manipulation
+ +const df = DataFrame.fromColumns({
+ name: ["Alice", "Bob"],
+ score: [90, 80],
+});
+
+// Promote "name" to the row index
+const indexed = df.setIndex("name");
+indexed.index.values; // ["Alice", "Bob"]
+indexed.columns.values; // ["score"]
+
+// Restore a numeric index
+const reset = indexed.resetIndex();
+reset.columns.values; // ["index", "score"]
+ + tsb is a TypeScript port of pandas, built from first principles. + — β Back to Playground +
+ +