Java 中的資料匯入
Anthony Markham
VP Quant Developer
import tech.tablesaw.api.*import tech.tablesaw.api.DoubleColumnimport tech.tablesaw.api.StringColumnimport tech.tablesaw.aggregate.*
// 傳統 Java 作法(繁瑣)
import java.util.Arrays;
import java.util.List;
List<String> names = Arrays.asList("Anna", "Bob", "Carlos");
List<Integer> ages = Arrays.asList(25, 34, 42);
// Creating from scratch
Table employees = Table.create("Employees")
.addColumns(
StringColumn.create("Name", "John", "Lisa", "Omar"),
DoubleColumn.create("Salary", 50000, 60000, 55000)
);
// From existing columns
StringColumn dept = StringColumn.create("Department",
"Sales", "Marketing", "Engineering");
Table departments = Table.create("Departments", dept);
addColumns() 與 create() 方法table.shape()table.columnNames()table.structure()table.first(n)、table.last(n)// Print dimensions
System.out.println(data.shape()); // [rows, columns]
[10, 4]
// 列印欄名
System.out.println(table.columnNames());
[Day, Temperature, Precipitation]
// 列印詳細結構
System.out.println(table.structure());
Structure of table
Index | Column Name | Column Type |
0 | Day | STRING |
1 | Temperature | DOUBLE |
2 | Precipitation | DOUBLE |
// 預覽前 3 列
System.out.println(table.first(3));
table
Day | Temperature | Precipitation |
Monday | 22.5 | 0 |
Tuesday | 24 | 2.5 |
Wednesday | 23.2 | 5.2 |
table.addColumns(newColumn)// 新增一個欄位
DoubleColumn bonus = DoubleColumn.create("Bonus", 1000, 1500, 2000);
employees = employees.addColumns(bonus);
// 移除欄位
employees = employees.removeColumns("StartDate");
// 重新命名欄位
employees.column("Salary").setName("AnnualSalary");
// 取得欄位型別
employees.column("Salary").type();
ColumnType.INTEGER
$$
| 方法/語法 | 說明 |
|---|---|
Table.create("TableName") |
以指定名稱建立新資料表 |
StringColumn.create("ColumnName", values) |
建立字串欄位 |
table.shape() |
回傳維度 [rows, columns] |
table.columnNames() |
回傳資料表中的欄名 |
table.structure() |
顯示資料表結構資訊 |
table.first(n) |
回傳前 n 列資料 |
table.last(n) |
回傳後 n 列資料 |
table.addColumns(newColumn) |
將新欄位加入資料表 |
Java 中的資料匯入