การนำเข้าข้อมูลใน Java
Anthony Markham
VP Quant Developer
import tech.tablesaw.api.*import tech.tablesaw.api.DoubleColumnimport tech.tablesaw.api.StringColumnimport tech.tablesaw.aggregate.*
// Traditional Java approach (cumbersome)
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]
// Print column names
System.out.println(table.columnNames());
[Day, Temperature, Precipitation]
// Print detailed structure
System.out.println(table.structure());
Structure of table
Index | Column Name | Column Type |
0 | Day | STRING |
1 | Temperature | DOUBLE |
2 | Precipitation | DOUBLE |
// Preview first three rows
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)// Add a new column
DoubleColumn bonus = DoubleColumn.create("Bonus", 1000, 1500, 2000);
employees = employees.addColumns(bonus);
// Remove a column
employees = employees.removeColumns("StartDate");
// Rename a column
employees.column("Salary").setName("AnnualSalary");
// Get the type of column
employees.column("Salary").type();
ColumnType.INTEGER
$$
| เมธอด/ซินแทกซ์ | คำอธิบาย |
|---|---|
Table.create("TableName") |
สร้างตารางใหม่ด้วยชื่อที่กำหนด |
StringColumn.create("ColumnName", values) |
สร้างคอลัมน์ชนิด string |
table.shape() |
คืนค่าขนาดในรูปแบบ [แถว, คอลัมน์] |
table.columnNames() |
คืนค่าชื่อคอลัมน์ทั้งหมดในตาราง |
table.structure() |
แสดงข้อมูลโครงสร้างของตาราง |
table.first(n) |
คืนค่า n แถวแรกของตาราง |
table.last(n) |
คืนค่า n แถวสุดท้ายของตาราง |
table.addColumns(newColumn) |
เพิ่มคอลัมน์ใหม่ลงในตาราง |
การนำเข้าข้อมูลใน Java