Search in sources :

Example 26 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class TableBuildingUtils method build.

public static Table build(List<String> columnNames, List<String[]> dataRows, ReadOptions options) {
    Table table = Table.create(options.tableName());
    if (dataRows.isEmpty()) {
        return table;
    }
    ColumnTypeDetector detector = new ColumnTypeDetector(options.columnTypesToDetect());
    Iterator<String[]> iterator = dataRows.iterator();
    ColumnType[] types = detector.detectColumnTypes(iterator, options);
    // If there are columnTypes configured by the user use them
    for (int i = 0; i < types.length; i++) {
        boolean hasColumnName = i < columnNames.size();
        Optional<ColumnType> configuredColumnType = options.columnTypeReadOptions().columnType(i, hasColumnName ? columnNames.get(i) : null);
        if (configuredColumnType.isPresent())
            types[i] = configuredColumnType.get();
    }
    for (int i = 0; i < columnNames.size(); i++) {
        table.addColumns(types[i].create(columnNames.get(i)));
    }
    for (int i = 0; i < dataRows.size(); i++) {
        for (int j = 0; j < table.columnCount(); j++) {
            table.column(j).appendCell(dataRows.get(i)[j]);
        }
    }
    return table;
}
Also used : Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType)

Example 27 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class SerializableDataSetTest method testDataset.

public void testDataset() {
    Table table = Table.read().csv(// 
    "Products,Sales,Market_Share\n" + // 
    "a,5500,3\n" + // 
    "b,12200,4\n" + "c,60000,33\n", "");
    ASTDataset ds = ASTDataset.newTablesawTable(table);
    equalsStringCopy(ds);
}
Also used : ASTDataset(org.matheclipse.io.expression.ASTDataset) Table(tech.tablesaw.api.Table)

Example 28 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class CrossTab method rowPercents.

private static Table rowPercents(Table xTabCounts) {
    Table pctTable = Table.create("Crosstab Row Proportions: ");
    StringColumn labels = StringColumn.create(LABEL_COLUMN_NAME);
    pctTable.addColumns(labels);
    for (int i = 0; i < xTabCounts.rowCount(); i++) {
        labels.append(xTabCounts.column(0).getString(i));
    }
    // create the new cols
    DoubleColumn[] newColumns = new DoubleColumn[xTabCounts.columnCount() - 1];
    for (int i = 1; i < xTabCounts.columnCount(); i++) {
        Column<?> column = xTabCounts.column(i);
        newColumns[i - 1] = DoubleColumn.create(column.name());
    }
    for (int i = 0; i < xTabCounts.rowCount(); i++) {
        double rowTotal = xTabCounts.numberColumn(xTabCounts.columnCount() - 1).getDouble(i);
        for (int c = 0; c < newColumns.length; c++) {
            if (rowTotal == 0) {
                newColumns[c].append(Double.NaN);
            } else {
                newColumns[c].append(xTabCounts.numberColumn(c + 1).getDouble(i) / rowTotal);
            }
        }
    }
    pctTable.addColumns(newColumns);
    return pctTable;
}
Also used : StringColumn(tech.tablesaw.api.StringColumn) Table(tech.tablesaw.api.Table) TreeBasedTable(com.google.common.collect.TreeBasedTable) DoubleColumn(tech.tablesaw.api.DoubleColumn)

Example 29 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class CrossTab method counts.

/**
 * Returns a table containing two-dimensional cross-tabulated counts for each combination of
 * values in {@code column1} and {@code column2}
 *
 * <p>
 *
 * @param table The table we're deriving the counts from
 * @param column1 A column in {@code table}
 * @param column2 Another column in {@code table}
 * @return A table containing the cross-tabs
 */
public static Table counts(Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2) {
    Table t = Table.create("Crosstab Counts: " + column1.name() + " x " + column2.name());
    t.addColumns(column1.type().create(LABEL_COLUMN_NAME));
    Table temp = table.sortOn(column1.name(), column2.name());
    int colIndex1 = table.columnIndex(column1.name());
    int colIndex2 = table.columnIndex(column2.name());
    com.google.common.collect.Table<String, String, Integer> gTable = TreeBasedTable.create();
    String a;
    String b;
    for (int row = 0; row < table.rowCount(); row++) {
        a = temp.column(colIndex1).getString(row);
        b = temp.column(colIndex2).getString(row);
        Integer cellValue = gTable.get(a, b);
        Integer value;
        if (cellValue != null) {
            value = cellValue + 1;
        } else {
            value = 1;
        }
        gTable.put(a, b, value);
    }
    for (String colName : gTable.columnKeySet()) {
        t.addColumns(IntColumn.create(colName));
    }
    t.addColumns(IntColumn.create("total"));
    int[] columnTotals = new int[t.columnCount()];
    for (String rowKey : gTable.rowKeySet()) {
        t.column(0).appendCell(rowKey);
        int rowSum = 0;
        for (String colKey : gTable.columnKeySet()) {
            Integer cellValue = gTable.get(rowKey, colKey);
            if (cellValue != null) {
                int colIdx = t.columnIndex(colKey);
                t.intColumn(colIdx).append(cellValue);
                rowSum += cellValue;
                columnTotals[colIdx] = columnTotals[colIdx] + cellValue;
            } else {
                t.intColumn(colKey).append(0);
            }
        }
        t.intColumn(t.columnCount() - 1).append(rowSum);
    }
    if (t.column(0).type().equals(ColumnType.STRING)) {
        t.column(0).appendCell("Total");
    } else {
        t.column(0).appendCell("");
    }
    int grandTotal = 0;
    for (int i = 1; i < t.columnCount() - 1; i++) {
        t.intColumn(i).append(columnTotals[i]);
        grandTotal = grandTotal + columnTotals[i];
    }
    t.intColumn(t.columnCount() - 1).append(grandTotal);
    return t;
}
Also used : Table(tech.tablesaw.api.Table) TreeBasedTable(com.google.common.collect.TreeBasedTable)

Example 30 with Table

use of tech.tablesaw.api.Table in project symja_android_library by axkr.

the class XlsxReader method createTable.

private Table createTable(Sheet sheet, TableRange tableArea, XlsxReadOptions options) {
    Optional<List<String>> optHeaderNames = getHeaderNames(sheet, tableArea);
    optHeaderNames.ifPresent(h -> tableArea.startRow++);
    List<String> headerNames = optHeaderNames.orElse(calculateDefaultColumnNames(tableArea));
    Table table = Table.create(options.tableName() + "#" + sheet.getSheetName());
    List<Column<?>> columns = new ArrayList<>(Collections.nCopies(headerNames.size(), null));
    for (int rowNum = tableArea.startRow; rowNum <= tableArea.endRow; rowNum++) {
        Row row = sheet.getRow(rowNum);
        for (int colNum = 0; colNum < headerNames.size(); colNum++) {
            int excelColNum = colNum + tableArea.startColumn;
            Cell cell = row.getCell(excelColNum, MissingCellPolicy.RETURN_BLANK_AS_NULL);
            Column<?> column = columns.get(colNum);
            String columnName = headerNames.get(colNum);
            if (cell != null) {
                if (column == null) {
                    column = createColumn(colNum, columnName, sheet, excelColNum, tableArea, options);
                    columns.set(colNum, column);
                    while (column.size() < rowNum - tableArea.startRow) {
                        column.appendMissing();
                    }
                }
                Column<?> altColumn = appendValue(column, cell);
                if (altColumn != null && altColumn != column) {
                    column = altColumn;
                    columns.set(colNum, column);
                }
            } else {
                boolean hasCustomizedType = options.columnTypeReadOptions().columnType(colNum, columnName).isPresent();
                if (column == null && hasCustomizedType) {
                    ColumnType columnType = options.columnTypeReadOptions().columnType(colNum, columnName).get();
                    column = columnType.create(columnName).appendMissing();
                    columns.set(colNum, column);
                } else if (hasCustomizedType) {
                    column.appendMissing();
                }
            }
            if (column != null) {
                while (column.size() <= rowNum - tableArea.startRow) {
                    column.appendMissing();
                }
            }
        }
    }
    columns.removeAll(Collections.singleton(null));
    table.addColumns(columns.toArray(new Column<?>[columns.size()]));
    return table;
}
Also used : Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) ArrayList(java.util.ArrayList) LongColumn(tech.tablesaw.api.LongColumn) DoubleColumn(tech.tablesaw.api.DoubleColumn) Column(tech.tablesaw.columns.Column) ArrayList(java.util.ArrayList) List(java.util.List) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

Table (tech.tablesaw.api.Table)42 StringColumn (tech.tablesaw.api.StringColumn)10 ArrayList (java.util.ArrayList)9 DoubleColumn (tech.tablesaw.api.DoubleColumn)8 IntColumn (tech.tablesaw.api.IntColumn)8 ColumnType (tech.tablesaw.api.ColumnType)7 Column (tech.tablesaw.columns.Column)7 TreeBasedTable (com.google.common.collect.TreeBasedTable)5 Map (java.util.Map)5 List (java.util.List)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 IExpr (org.matheclipse.core.interfaces.IExpr)3 ExprColumn (tech.tablesaw.api.ExprColumn)3 Row (tech.tablesaw.api.Row)3 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 IAST (org.matheclipse.core.interfaces.IAST)2 Strings (com.google.common.base.Strings)1 Lists (com.google.common.collect.Lists)1