資料操作與分析

Java 中的資料匯入

Anthony Markham

VP Quant Developer

欄位選取

  • .selectColumns() 會建立一個「新的」Table 物件
// 依欄位名稱選取特定欄位
Table nameAndSalary = employees.selectColumns("Name", "Salary");
// 依欄位型別選取
Table numericColumns = employees.selectColumns(
    column -> column.type().equals(ColumnType.DOUBLE) ||
    column.type().equals(ColumnType.INTEGER));
Java 中的資料匯入

資料篩選

  • .where() 篩選列
  • 回傳新的 Table 物件
// 使用單一條件篩選
Table seniors = employees.where(
    employees.intColumn("Age").isGreaterThanOrEqualTo(65));
// 使用多個條件篩選
Table targetGroup = employees.where(
    employees.intColumn("Age").isBetweenInclusive(30, 50)
        .and(employees.doubleColumn("Salary")
            .isGreaterThan(75000)));
Java 中的資料匯入

資料排序

  • .sortOn() 整理資料
    • 以遞增排序
  • 回傳新的已排序資料表
  • 遞減排序用 .sortDescendingOn()
// 以單一欄位排序(遞增)
Table sortedBySalary = employees.sortOn("Salary");
// 以多欄位排序(自訂方向)
Table complexSort = employees
    .sortOn("Department") 
    .sortDescendingOn("Salary");
Java 中的資料匯入

使用 summarize 聚合

  • .summarize() 計算摘要統計
  • 回傳新的資料表
import tech.tablesaw.aggregate.AggregateFunctions.*;
// 彙總薪資
Table deptSummary = employees.summarize("Salary", mean, count, max).apply();
Mean [Salary] Count [Salary] Max [Salary]
113606.20299999935 1000 199793
  • 預設套用到所有數值欄位
Java 中的資料匯入

使用 summarize 聚合

  • 可進行更進階的聚合
// 多種聚合
Table complexSummary = employees.summarize(
    "Salary", "Age", 
    mean, median, min, max).apply();
Mean [Salary] Median [Salary] Min [Salary] Max [Salary] Mean [Age] Median [Age] Min [Age] Max [Age]
113606.2029 112667 30301 199793 45.6699 46 22 70
Java 中的資料匯入

核心操作方法

  • .select():選取特定欄位
    Table selected = employees.select("Name", "Salary");
    
  • .where():依條件篩選列
    Table filtered = employees.where(condition);
    
  • .sortOn():依欄位排序
    Table sorted = employees.sortOn("Department", "Salary");
    
  • .summarize():計算統計
    Table summary = employees.summarize("Salary", mean, max).apply();
    
Java 中的資料匯入

一起來練習吧!

Java 中的資料匯入

Preparing Video For Download...