Example 04
beginner
04
DataFrame
Basics

DataFrame Basics

DataFrames in Deepbox provide a tabular API for data analysis. This example covers creating DataFrames from objects, selecting columns with .select(), filtering rows with .filter(), sorting by column values with .sortBy(), adding new derived columns, accessing individual columns as Series with .get(), and converting between DataFrames and arrays. You will also learn to use .head() and .tail() to preview data, .shape to check dimensions, and .columns to list column names. These operations mirror what you would do in a SQL query — select, filter, sort, project — but with full TypeScript type safety.

Deepbox Modules Used

deepbox/dataframe

What You Will Learn

  • Create DataFrames from column-oriented JavaScript objects
  • Select columns, filter rows, sort by values
  • Use .head() / .tail() for quick previews
  • Access shape and column metadata

Source Code

04-dataframe-basics/index.ts
1import { DataFrame } from "deepbox/dataframe";23console.log("=== DataFrame Basics ===\n");45const df = new DataFrame({6  name: ["Alice", "Bob", "Charlie", "David", "Eve"],7  age: [25, 30, 35, 28, 32],8  salary: [50000, 60000, 75000, 55000, 70000],9  dept: ["Engineering", "Sales", "Engineering", "HR", "Sales"],10});1112console.log("Shape:", df.shape);         // [5, 4]13console.log("Columns:", df.columns);     // ["name", "age", ...]1415console.log("\nFull table:");16console.log(df.toString());1718// Select specific columns19console.log("\nSelect name + salary:");20console.log(df.select(["name", "salary"]).toString());2122// Filter rows23const engineers = df.filter(row => row.dept === "Engineering");24console.log("\nEngineers:");25console.log(engineers.toString());2627// Sort by salary descending28const sorted = df.sortBy("salary", false);29console.log("\nSorted by salary (desc):");30console.log(sorted.toString());3132// Head / tail33console.log("\nFirst 2 rows:");34console.log(df.head(2).toString());

Console Output

$ npx tsx 04-dataframe-basics/index.ts
=== DataFrame Basics ===

Shape: [5, 4]
Columns: ["name", "age", "salary", "dept"]

Full table:
┌─────────┬─────┬────────┬─────────────┐
│ name    │ age │ salary │ dept        │
├─────────┼─────┼────────┼─────────────┤
│ Alice   │  25 │  50000 │ Engineering │
│ Bob     │  30 │  60000 │ Sales       │
│ Charlie │  35 │  75000 │ Engineering │
│ David   │  28 │  55000 │ HR          │
│ Eve     │  32 │  70000 │ Sales       │
└─────────┴─────┴────────┴─────────────┘

Engineers:
┌─────────┬─────┬────────┬─────────────┐
│ name    │ age │ salary │ dept        │
├─────────┼─────┼────────┼─────────────┤
│ Alice   │  25 │  50000 │ Engineering │
│ Charlie │  35 │  75000 │ Engineering │
└─────────┴─────┴────────┴─────────────┘