Importing Data in 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() and create() methodstable.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
$$
| Method/Syntax | Description |
|---|---|
Table.create("TableName") |
Creates a new table with the given name |
StringColumn.create("ColumnName", values) |
Creates a string column |
table.shape() |
Returns dimensions as [rows, columns] |
table.columnNames() |
Returns column names in the table |
table.structure() |
Displays table structure information |
table.first(n) |
Returns the first n rows of the table |
table.last(n) |
Returns the last n rows of the table |
table.addColumns(newColumn) |
Adds a new column to the table |
Importing Data in Java