Tablesaw 的資料結構

Java 中的資料匯入

Anthony Markham

VP Quant Developer

核心資料結構

  • Table: 主要容器(類似 DataFrame)
Table employees = Table.create("Employees");
  • Column: 儲存一致型別的值
StringColumn nameCol = StringColumn.create("Name");
  • Row: 代表單筆紀錄
Row firstRow = employees.row(0);
Java 中的資料匯入

Table 方法

String tableName = employees.name();
employees
// 列與欄數量
int rowCount = employees.rowCount();
int columnCount = employees.columnCount();
1000
5
Java 中的資料匯入

欄位型別

  • 強型別=每個欄都有固定型別
    • 提升效能並使程式更易除錯
  • 範例:
    • StringColumn:文字資料
    • IntColumnDoubleColumn:數值
    • BooleanColumn:True/False 值
    • 時間序列資料:
      • DateColumn:行事曆日期(2024-03-05
      • DateTimeColumn:日期時間(2024-03-05T14:32
Java 中的資料匯入

欄位型別的操作

  • 各型別提供專用操作
// 在 DoubleColumn 上使用 .mean()
DoubleColumn salary = employees.column("Salary");
double averageSalary = salary.mean();
Java 中的資料匯入

存取資料

// 取得特定型別的欄
StringColumn names = employees.stringColumn("Name");

// 取得一般欄 names = employees.column("Name");
// 從欄位取得單一值 String firstPerson = names.get(0);
// 取得整列資料 Row firstRow = employees.row(0);
// 從列取得單一值 double salary = firstRow.getDouble("Salary");
Java 中的資料匯入

Selections(選擇集)

  • 符合條件的列索引集合
  • 範例:
    • .isGreaterThan().isLessThan()
    • .isEqualTo()
    • .isAfter()

$$

// 建立列的選擇條件
Selection highEarners = employees.doubleColumn("Salary")
    .isGreaterThan(70000);
Java 中的資料匯入

用 Selection 過濾

// 建立列的選擇條件
Selection highEarners = employees.doubleColumn("Salary")
    .isGreaterThan(70000);


// 套用選擇條件以取得過濾後的表格 Table highPaidEmployees = employees.where(highEarners);

$$

$$

  • .where() 會回傳新的表格
Java 中的資料匯入

布林運算

  • .and().or() 組合選擇條件
Selection recentHires = employees.dateColumn("HireDate")
    .isAfter(LocalDate.of(2020, 1, 1));

Selection highPaidRecent = highEarners.and(recentHires);
Java 中的資料匯入

一起來練習吧!

Java 中的資料匯入

Preparing Video For Download...