SuiteTable
SuiteTable provides utility methods for managing and manipulating tables and columns using Tablesaw Framework.
Steps for Creating a Table:
- Create Table: Use
SuiteTable.create("tablename"). - Define Columns: Use methods such as
addStringColumn(),addIntColumn(), andaddDateColumn()to add columns to the table.
table.addStringColumn("columnName")
- Define Rows as maps:
var row = { "column1": "field1", "column2": "field2" } - Add Rows to Table: use SuiteTable's addRow method:
table.addRow(row);
// create a new table
var table = SuiteTable.create("MyTable");
// add columns to table
table.addStringColumn("name");
table.addIntColumn("age");
table.addInstantColumn("joined");
table.addDateTimeColumn("birthday");
table.addBooleanColumn("customer");
// define two rows
var row1 = {
"Name": "tony",
"age": 20,
"joined": Instant.now(),
"birthday": LocalDateTime.now(),
"customer": true
};
var row2 = {
"Name" : "Mark",
"age": 20,
"joined": Instant.parse("2023-10-20T12:00:00Z"),
"birthday": LocalDateTime.of(2023, 10, 24, 15, 30),
"customer": false
};
// add rows to table
table.addRow(row1);
table.addRow(row2);
return table;
Adding Columns
Add a column to a table with a single add<Type>Column method.
Note: The columns appear in the table in the order they were added.
Supported column types: String, Integer, Double, Boolean, Instant, DateTime, Date, and Time.
addStringColumn(columnName: String): void
Add a StringColumn with the specified name to a Table instance.
Parameters:
columnName(Type: String) - The name of the StringColumn.
Example:
table.addStringColumn("name");
addIntColumn(columnName: String): void
Add an IntColumn with the specified name to a Table instance.
Parameters:
columnName(Type: String) - The name of the IntColumn.
Example:
table.addIntColumn("age");
addDoubleColumn(columnName: String): void
Add a DoubleColumn with the specified name to a Table instance.
Parameters:
columnName(Type: String) - The name of the DoubleColumn.
Example:
table.addDoubleColumn("gpa");
Similarly for the rest of the ColumnTypes.
Adding Rows
Note: The rows appear in the table in the order they were added.
To add a row, first create a table instance and add at least one column. Then define each row as a hash map where the keys are column names and the values are row values.
var row = { "column1": value1,
"column2": value2,
"column3": value3,
...
};
Then, you can add the row to your table using the addRow method:
addRow(HashMap<String, Object> row): Row
Add a row of data to the table instance and return the row.
Parameters:
row(Type: HashMap<String, Object>) - A map representing the data to be added to the row.
Example:
var row1 = { // HashMap
"Name" : "Mark",
"age": 20,
"joined": instant,
"birthday": datetime,
"customer": true
};
table.addRow(row1);
Table Information
Methods for retrieving table information such as the table name, column names, and row counts.
name(): String
Get the name of the table.
Returns:
- A String representing the name of the table.
Example:
var tableName = table.name();
getColumnNames(): List<String>
Get the list of column names in the table.
Returns:
- A List<String> representing the column names.
Example:
var columnNames = table.getColumnNames();
columnCount(): Integer
Get the number of columns in the table.
Returns:
- An Integer representing the total number of columns in the table.
Example:
var numOfColumns = table.columnCount();
Use columnCount() when you need to validate the table structure before iterating over rows or applying transformations.
rowCount(): Integer
Get the number of rows in the table.
Returns:
- An Integer representing the total number of rows in the table.
Example:
var numOfRows = table.rowCount();
Use rowCount() to confirm how many records were loaded or produced before you continue with filtering, export, or downstream processing.
columnIndex(columnName: String): Integer - Returns the index of a column by name.
var table = SuiteTable.create("MyTable");
table.addStringColumn("key"); // column index 0
table.addDoubleColumn("rate"); // column index 1
table.addStringColumn("lastupdate"); // column index 2
// Specify the column name you want to find the index for
var columnIndex = table.columnIndex("rate");
// Get the index of the column by name
var columnIndex = originalTable.columnIndex(columnNameToFind);
// columnIndex is 1
row(rowIndex: Integer): Row - Returns the Row object at the specified index.
var rowIndex = 3; // Example index
var row = table.row(rowIndex);
where(selection: Selection): Table - Returns a new table containing rows that match the selection criteria.
var selection = table.stringColumn("name").isEqualTo("Alice");
var filteredTable = table.where(selection);
return filteredTable;
// filteredTable now has one row: Alice: 25
stringColumn(columnIndex: Integer): StringColumn - Returns a StringColumn for the specified column.
// Specify the index of the column for which you want to get a StringColumn
var columnIndex = 3; // Replace with the actual index
// Get the StringColumn for the specified column index
var stringColumn = originalTable.stringColumn(columnIndex);
first(nRows: Integer): Table - Returns a new table containing the first nRows of data.
var first10Rows = table.first(10);
last(nRows: Integer): Table - Returns a new table containing the last nRows of data.
// Specify the number of rows you want to retrieve from the end
var nRows = 5; // Replace with the actual number of rows you need
// Get a new table containing the last nRows of data
var lastTable = originalTable.last(nRows);
inRange(rowStart: Integer, rowEnd: Integer): Table - Returns a new table containing rows within the specified range.
// Specify the range of rows you want to extract
var rowStart = 2; // Replace with the actual starting row index
var rowEnd = 6; // Replace with the actual ending row index
// Get a new table containing rows within the specified range
var rangeTable = originalTable.inRange(rowStart, rowEnd);
sortOn(columnIndex: Integer): Table - Sorts the table based on the specified column index.
var sortedTable = table.sortOn(columnIndex);
removeColumns(...columnIndex: Integer[]): Table - Removes specified columns from the table.
var columnsToRemove = [1, 3]; // Example column indices to remove
var modifiedTable = table.removeColumns(...columnsToRemove);
emptyCopy(): Table - Returns a table with the same columns but no data.
var emptyCopy = table.emptyCopy();
sampleX(proportion: Double): Table - Returns a table with a sample based on the given proportion.
var proportion = 0.2; // Example proportion
var sampledTable = table.sampleX(proportion);
sampleN(nRows: Integer): Table - Returns a table with a random sample of the specified number of rows.
var numRowsToSample = 5; // Example number of rows to sample
var sampledTable = table.sampleN(numRowsToSample);
summary(): Table - Returns a table containing summary statistics for the columns in the relation.
var summaryTable = table.summary();
Examples
var dataFrame = NodeInputReader.inputAsDataFrame();
for (var i = 0; i < dataFrame.rowCount(); i++) {
var row = dataFrame.row(i);
var objectId = row.getString("object_id");
var display = row.getString("display");
}
// Retrieve exchange rates from the API
var request = HttpUtil.get("https://boi.org.il/PublicApi/GetExchangeRates");
var jsonContent = JsonUtil.parse(request.asString().getBody());
var exchanges = jsonContent.exchangeRates;
// Create a table to store the data
var table = SuiteTable.create("MyTable");
table.addStringColumn("key");
table.addDoubleColumn("rate");
table.addStringColumn("lastupdate");
// Populate the table with data from the API
for (var i = 0; i < exchanges.size(); i++) {
var item = exchanges[i];
var key = item.key.asText();
var rate = item.currentExchangeRate.asDouble();
var date = item.lastUpdate.asText();
var row = {
"key": key,
"rate": rate,
"lastUpdate": date
};
table.addRow(row);
}
// Filter table with some type of selection example:
var selection = table.stringColumn("key").isEqualTo("USD");
var filteredTable = table.where(selection);
// Return the filtered table
return filteredTable;