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 .sort(), and accessing individual columns as Series with .get().
Deepbox Modules Used
deepbox/dataframeWhat 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";23const expectNumber = (value: unknown): number => {4 if (typeof value !== "number") {5 throw new Error("Expected number");6 }7 return value;8};910console.log("=== DataFrame Basics ===\n");1112// Create a DataFrame from an object with column arrays1314// Creating a DataFrame from an object15const df = new DataFrame({16 name: ["Alice", "Bob", "Charlie", "David", "Eve"],17 age: [25, 30, 35, 28, 32],18 salary: [50000, 60000, 75000, 55000, 70000],19 department: ["IT", "HR", "IT", "Sales", "HR"],20});2122// Display the complete DataFrame23console.log("Full DataFrame:");24console.log(df.toString());2526// Display DataFrame dimensions27console.log(`\nShape: ${df.shape[0]} rows × ${df.shape[1]} columns`);2829// Access individual columns30console.log("\nColumn Access:");31console.log(`Columns: ${df.columns.join(", ")}\n`);3233// Accessing columns34// Get a single column as a Series35const ages = df.get("age");36console.log("Age column:");37console.log(`${ages.toString()}\n`);3839// Selecting specific columns40// Select multiple columns at once41const subset = df.select(["name", "salary"]);42console.log("\nSelecting Multiple Columns:");43console.log("Selected columns (name, salary):");44console.log(`${subset.toString()}\n`);4546// Filtering rows47// Filter for high earners (salary > 60000)48const highEarners = df.filter((row) => expectNumber(row.salary) > 60000);49console.log("Employees with salary > 60000:");50console.log(`${highEarners.toString()}\n`);5152// Sorting53// Sort by salary in descending order54const sortedBySalary = df.sort("salary", false);55console.log("\nSorting:");56console.log("Sorted by salary (descending):");57console.log(`${sortedBySalary.toString()}\n`);5859// Head and tail60console.log("\nFiltering Rows:");61console.log("First 3 rows:");62console.log(`${df.head(3).toString()}\n`);6364console.log("Last 2 rows:");65console.log(`${df.tail(2).toString()}\n`);6667console.log("✓ DataFrame basics complete!");Console Output
$ npx tsx 04-dataframe-basics/index.ts
=== DataFrame Basics ===
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
Column Access:
Columns: name, age, salary, department
Age column:
0 25
1 30
2 35
3 28
4 32
Name: age, Length: 5
Selecting Multiple Columns:
Selected columns (name, salary):
name salary
0 Alice 50000
1 Bob 60000
2 Charlie 75000
3 David 55000
4 Eve 70000
Employees with salary > 60000:
name age salary department
2 Charlie 35 75000 IT
4 Eve 32 70000 HR
Sorting:
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
Filtering Rows:
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
✓ DataFrame basics complete!