Tidy-TS Logo

Combining DataFrames

Combine data from multiple sources with bindRows and other combining operations. Learn how to merge datasets effectively.

Basic bindRows
Combine two DataFrames by stacking rows

bindRows is the main way to combine DataFrames by adding rows from one DataFrame to another. It's great for combining datasets with the same structure.

import { createDataFrame } from "@tidy-ts/dataframe";

const df1 = createDataFrame([
  { id: 1, name: "Alice", age: 25 },
  { id: 2, name: "Bob", age: 30 },
]);

const df2 = createDataFrame([
  { id: 3, name: "Charlie", age: 35 },
  { id: 4, name: "Diana", age: 28 },
]);

// Combine DataFrames with bindRows
const combined = df1.bindRows(df2);
combined.print("Combined DataFrames:");
import { createDataFrame } from "@tidy-ts/dataframe";

const df1 = createDataFrame([
  { id: 1, name: "Alice", age: 25 },
  { id: 2, name: "Bob", age: 30 },
]);

const df2 = createDataFrame([
  { id: 3, name: "Charlie", age: 35 },
  { id: 4, name: "Diana", age: 28 },
]);

// Combine DataFrames with bindRows
const combined = df1.bindRows(df2);
combined.print("Combined DataFrames:");
Multiple DataFrames
Combine multiple DataFrames at once

You can combine multiple DataFrames in a single bindRows call. This is more efficient than chaining multiple bindRows operations.

const df1 = createDataFrame([{ id: 1, name: "Alice" }]);
const df2 = createDataFrame([{ id: 2, name: "Bob" }]);
const df3 = createDataFrame([{ id: 3, name: "Charlie" }]);

// Combine multiple DataFrames at once
const combined = df1.bindRows(df2, df3);
combined.print("Multiple DataFrames combined:");
const df1 = createDataFrame([{ id: 1, name: "Alice" }]);
const df2 = createDataFrame([{ id: 2, name: "Bob" }]);
const df3 = createDataFrame([{ id: 3, name: "Charlie" }]);

// Combine multiple DataFrames at once
const combined = df1.bindRows(df2, df3);
combined.print("Multiple DataFrames combined:");
Different Columns
Handle DataFrames with different column structures

bindRows gracefully handles DataFrames with different columns. It creates a union of all columns, filling missing values with undefined where needed.

const df1 = createDataFrame([
  { id: 1, name: "Alice", age: 25 },
]);

const df2 = createDataFrame([
  { id: 2, name: "Bob", age: 30, salary: 50000 },
]);

// Handle DataFrames with different columns
const combined = df1.bindRows(df2);
combined.print("Different columns handled:");
const df1 = createDataFrame([
  { id: 1, name: "Alice", age: 25 },
]);

const df2 = createDataFrame([
  { id: 2, name: "Bob", age: 30, salary: 50000 },
]);

// Handle DataFrames with different columns
const combined = df1.bindRows(df2);
combined.print("Different columns handled:");
Spread Operator
Alternative approach using spread operator

You can also use the traditional spread operator to combine DataFrames, though bindRows is generally preferred for its clarity and type safety.

const df1 = createDataFrame([{ id: 1, name: "Alice" }]);
const df2 = createDataFrame([{ id: 2, name: "Bob" }]);

// Alternative approach using spread operator
const combined = createDataFrame([...df1, ...df2]);
combined.print("Spread operator combination:");
const df1 = createDataFrame([{ id: 1, name: "Alice" }]);
const df2 = createDataFrame([{ id: 2, name: "Bob" }]);

// Alternative approach using spread operator
const combined = createDataFrame([...df1, ...df2]);
combined.print("Spread operator combination:");