Skip to main content
DataFrames provide a powerful interface for working with tabular data. This guide covers creation, selection, filtering, and sorting.

Working with DataFrames

1
Create a DataFrame
2
Create a DataFrame from an object with column arrays:
3
import { DataFrame } from "deepbox/dataframe";

const df = new DataFrame({
  name: ["Alice", "Bob", "Charlie", "David", "Eve"],
  age: [25, 30, 35, 28, 32],
  salary: [50000, 60000, 75000, 55000, 70000],
  department: ["IT", "HR", "IT", "Sales", "HR"],
});

console.log("Full DataFrame:");
console.log(df.toString());
console.log(`\nShape: ${df.shape[0]} rows × ${df.shape[1]} columns`);
4
Output:
5
Full DataFrame:
     name      age  salary  department
0   Alice       25   50000          IT
1     Bob       30   60000          HR
2 Charlie       35   75000          IT
3   David       28   55000       Sales
4     Eve       32   70000          HR

Shape: 5 rows × 4 columns
6
Access columns
7
Retrieve individual or multiple columns:
8
// Get column names
console.log("Columns:", df.columns.join(", "));

// Get a single column as a Series
const ages = df.get("age");
console.log("Age column:");
console.log(ages.toString());

// Select multiple columns
const subset = df.select(["name", "salary"]);
console.log("\nSelected columns (name, salary):");
console.log(subset.toString());
9
Output:
10
Columns: name, age, salary, department

Age column:
0    25
1    30
2    35
3    28
4    32

Selected columns (name, salary):
     name  salary
0   Alice   50000
1     Bob   60000
2 Charlie   75000
3   David   55000
4     Eve   70000
11
Filter rows
12
Filter data based on conditions:
13
const expectNumber = (value: unknown): number => {
  if (typeof value !== "number") {
    throw new Error("Expected number");
  }
  return value;
};

// Filter for high earners
const highEarners = df.filter((row) => expectNumber(row.salary) > 60000);
console.log("Employees with salary > 60000:");
console.log(highEarners.toString());
14
Output:
15
Employees with salary > 60000:
     name  age  salary department
2 Charlie   35   75000         IT
4     Eve   32   70000         HR
16
Sort data
17
Sort by any column in ascending or descending order:
18
// Sort by salary (descending)
const sortedBySalary = df.sort("salary", false);
console.log("Sorted by salary (descending):");
console.log(sortedBySalary.toString());
19
Output:
20
Sorted by salary (descending):
     name  age  salary department
2 Charlie   35   75000         IT
4     Eve   32   70000         HR
1     Bob   30   60000         HR
3   David   28   55000      Sales
0   Alice   25   50000         IT
21
View subsets
22
Use head() and tail() to preview data:
23
console.log("First 3 rows:");
console.log(df.head(3).toString());

console.log("\nLast 2 rows:");
console.log(df.tail(2).toString());
24
Output:
25
First 3 rows:
     name  age  salary department
0   Alice   25   50000         IT
1     Bob   30   60000         HR
2 Charlie   35   75000         IT

Last 2 rows:
   name  age  salary department
3 David   28   55000      Sales
4   Eve   32   70000         HR

Next Steps

GroupBy Operations

Aggregate and analyze data by groups

Data Analysis

Learn comprehensive data analysis techniques

Build docs developers (and LLMs) love