Search in sources :

Example 1 with TableSlice

use of tech.tablesaw.table.TableSlice in project symja_android_library by axkr.

the class PivotTable method pivot.

public static Table pivot(Table table, CategoricalColumn<?> column1, CategoricalColumn<?> column2, NumericColumn<?> values, AggregateFunction<?, ?> aggregateFunction) {
    TableSliceGroup tsg = table.splitOn(column1);
    Table pivotTable = Table.create("Pivot: " + column1.name() + " x " + column2.name());
    pivotTable.addColumns(column1.type().create(column1.name()));
    List<String> valueColumnNames = getValueColumnNames(table, column2);
    for (String colName : valueColumnNames) {
        pivotTable.addColumns(DoubleColumn.create(colName));
    }
    int valueIndex = table.columnIndex(column2);
    int keyIndex = table.columnIndex(column1);
    String key;
    for (TableSlice slice : tsg.getSlices()) {
        key = String.valueOf(slice.get(0, keyIndex));
        pivotTable.column(0).appendCell(key);
        Map<String, Double> valueMap = getValueMap(column1, column2, values, valueIndex, slice, aggregateFunction);
        for (String columnName : valueColumnNames) {
            Double aDouble = valueMap.get(columnName);
            NumericColumn<?> pivotValueColumn = pivotTable.numberColumn(columnName);
            if (aDouble == null) {
                pivotValueColumn.appendMissing();
            } else {
                pivotValueColumn.appendObj(aDouble);
            }
        }
    }
    return pivotTable;
}
Also used : Table(tech.tablesaw.api.Table) TableSliceGroup(tech.tablesaw.table.TableSliceGroup) TableSlice(tech.tablesaw.table.TableSlice)

Example 2 with TableSlice

use of tech.tablesaw.table.TableSlice in project symja_android_library by axkr.

the class DoubleArrays method to2dArray.

public static double[][] to2dArray(TableSliceGroup views, int columnNumber) {
    int viewCount = views.size();
    double[][] allVals = new double[viewCount][];
    for (int viewNumber = 0; viewNumber < viewCount; viewNumber++) {
        TableSlice view = views.get(viewNumber);
        allVals[viewNumber] = new double[view.rowCount()];
        NumericColumn<?> numberColumn = view.numberColumn(columnNumber);
        for (int r = 0; r < view.rowCount(); r++) {
            allVals[viewNumber][r] = numberColumn.getDouble(r);
        }
    }
    return allVals;
}
Also used : TableSlice(tech.tablesaw.table.TableSlice)

Aggregations

TableSlice (tech.tablesaw.table.TableSlice)2 Table (tech.tablesaw.api.Table)1 TableSliceGroup (tech.tablesaw.table.TableSliceGroup)1