Working with DataFrames
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`);
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
// 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());
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
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());
// Sort by salary (descending)
const sortedBySalary = df.sort("salary", false);
console.log("Sorted by salary (descending):");
console.log(sortedBySalary.toString());
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
console.log("First 3 rows:");
console.log(df.head(3).toString());
console.log("\nLast 2 rows:");
console.log(df.tail(2).toString());
Next Steps
GroupBy Operations
Aggregate and analyze data by groups
Data Analysis
Learn comprehensive data analysis techniques