Data transformation patterns

Importing Data in Java

Anthony Markham

VP Quant Developer

Map function

  • Transforms each element in a column
DoubleColumn celsius = DoubleColumn.create("Celsius", 0, 10, 20, 30);

DoubleColumn fahrenheit = celsius.map(c -> c * 9.0/5.0 + 32); table.addColumns(fahrenheit.setName("Fahrenheit"));
Celsius Fahrenheit
0.0 32.0
10.0 50.0
20.0 68.0
30.0 86.0
Importing Data in Java

Reduce function

  • Combines all the values in a column into a single result
  • Applies an accumulator pattern (like sum, max, or custom logic)
  • Useful for statistical and analytical operations
// Find the total sales
double totalSales = table.doubleColumn("Sales").reduce(0, Double::sum);
250000
// Find the maximum
double largeSales = table.doubleColumn("Sales").reduce(0, (acc, x) -> acc + (x > 5000 ? 1 : 0));
68
Importing Data in Java

Row iteration with forEach

  • Iterates through each row in a table
DoubleColumn difference = DoubleColumn.create("Difference");
table.forEach(row -> {
    double celsius = row.getDouble("Celsius");
    double fahrenheit = row.getDouble("Fahrenheit");
    difference.append(fahrenheit - celsius);
});

table.addColumns(difference);
Celsius Fahrenheit Difference
0.0 32.0 32.0
10.0 50.0 40.0
20.0 68.0 48.0
Importing Data in Java

Transformation pipelines

  • Chain multiple operations together
  • Increase the readability and maintainability of our code ✅
  • Efficient data processing ✅
Table result = originalTable
    .where(numberColumn("Age").isGreaterThan(18)) // Filter on Age > 18

.addColumns( numberColumn("Income").map(i -> i * 1.1).setName("AdjustedIncome") );
// Calculate average income double avgIncome = result.doubleColumn("AdjustedIncome") .reduce(0.0, Double::sum) / result.rowCount();
Importing Data in Java

Recap

  • map() - apply a function to transform column values
  • forEach() - iterate through rows to access multiple columns
  • reduce() - aggregate and summarize data into single values

An image that shows the three data transformation functions

Importing Data in Java

Let's practice!

Importing Data in Java

Preparing Video For Download...