05
DataFrame
GroupBy
Aggregation
DataFrame GroupBy & Aggregation
GroupBy is one of the most powerful DataFrame operations. This example demonstrates splitting a dataset into groups based on a column value, then applying aggregate functions (mean, sum, count, min, max) to each group independently.
Deepbox Modules Used
deepbox/dataframeWhat You Will Learn
- Group rows by column values with .groupBy()
- Apply aggregate functions: mean, sum, count, min, max
- The result is a new DataFrame with one row per group
- Equivalent to SQL GROUP BY ... agg()
Source Code
05-dataframe-groupby/index.ts
1import { DataFrame } from "deepbox/dataframe";23console.log("=== DataFrame GroupBy & Aggregation ===\n");45// Create sample sales data6const sales = new DataFrame({7 product: ["Laptop", "Mouse", "Laptop", "Keyboard", "Mouse", "Laptop", "Keyboard", "Mouse"],8 region: ["North", "North", "South", "North", "South", "North", "South", "South"],9 quantity: [5, 20, 3, 15, 25, 4, 10, 30],10 revenue: [5000, 400, 3000, 450, 500, 4000, 300, 600],11});1213// Display the raw data14console.log("Sales Data:");15console.log(`${sales.toString()}\n`);1617// Group by a single column and aggregate18console.log("\nGroup by single column");19// Group by product and calculate total sales20const byProduct = sales.groupBy("product");21const productStats = byProduct.agg({22 quantity: "sum",23 revenue: "sum",24});2526console.log("Sales by Product:");27console.log(`${productStats.toString()}\n`);2829// Group by region30// Calculate average sales by region31const byRegion = sales.groupBy("region");32const regionStats = byRegion.agg({33 quantity: "mean",34 revenue: "mean",35});3637console.log("Average Sales by Region:");38console.log(`${regionStats.toString()}\n`);3940// Perform multiple aggregations at once41console.log("\nMultiple aggregations");42// Calculate sum, mean, and count for each product43const detailedStats = byProduct.agg({44 quantity: "sum",45 revenue: "mean",46});4748console.log("Detailed Product Statistics:");49console.log(`${detailedStats.toString()}\n`);5051console.log("✓ GroupBy operations complete!");Console Output
$ npx tsx 05-dataframe-groupby/index.ts
=== DataFrame GroupBy & Aggregation ===
Sales Data:
product region quantity revenue
0 Laptop North 5 5000
1 Mouse North 20 400
2 Laptop South 3 3000
3 Keyboard North 15 450
4 Mouse South 25 500
5 Laptop North 4 4000
6 Keyboard South 10 300
7 Mouse South 30 600
Group by single column
Sales by Product:
product quantity revenue
0 Laptop 12 12000
1 Mouse 75 1500
2 Keyboard 25 750
Average Sales by Region:
region quantity revenue
0 North 11 2462.5
1 South 17 1100
Multiple aggregations
Detailed Product Statistics:
product quantity revenue
0 Laptop 12 4000
1 Mouse 75 500
2 Keyboard 25 375
✓ GroupBy operations complete!