Search in sources :

Example 1 with TableSliceGroup

use of tech.tablesaw.table.TableSliceGroup 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 TableSliceGroup

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

the class Summarizer method having.

public Table having(Function<Table, Selection> selection) {
    Preconditions.checkState(groupColumnNames.length > 0, "Cannot perform having() on summary that has not been grouped first");
    if (groupColumnNames[0].equals(GROUP_COL_TEMP_NAME)) {
        IntColumn groupColumn = temp.intColumn(GROUP_COL_TEMP_NAME);
        TableSliceGroup group = StandardTableSliceGroup.create(temp, groupColumn);
        return summarizeForHaving(group, selection);
    } else {
        TableSliceGroup group = StandardTableSliceGroup.create(temp, groupColumnNames);
        return summarizeForHaving(group, selection);
    }
}
Also used : StandardTableSliceGroup(tech.tablesaw.table.StandardTableSliceGroup) TableSliceGroup(tech.tablesaw.table.TableSliceGroup) IntColumn(tech.tablesaw.api.IntColumn)

Example 3 with TableSliceGroup

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

the class Summarizer method apply.

/**
 * Returns the result of applying to the functions to all the values in the appropriate column
 * TODO add a test that uses a non numeric return type with apply
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public Table apply() {
    if (groupColumnNames.length > 0) {
        TableSliceGroup group = StandardTableSliceGroup.create(temp, groupColumnNames);
        return summarize(group);
    } else {
        List<Table> results = new ArrayList<>();
        ArrayListMultimap<String, AggregateFunction<?, ?>> reductionMultimap = getAggregateFunctionMultimap();
        for (String name : reductionMultimap.keys()) {
            List<AggregateFunction<?, ?>> reductions = reductionMultimap.get(name);
            Table table = TableSliceGroup.summaryTableName(temp);
            for (AggregateFunction function : reductions) {
                Column column = temp.column(name);
                Object result = function.summarize(column);
                ColumnType type = function.returnType();
                Column newColumn = type.create(TableSliceGroup.aggregateColumnName(name, function.functionName()));
                if (result instanceof Number) {
                    Number number = (Number) result;
                    newColumn.append(number.doubleValue());
                } else {
                    newColumn.append(result);
                }
                table.addColumns(newColumn);
            }
            results.add(table);
        }
        return (combineTables(results));
    }
}
Also used : Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) QuerySupport.numberColumn(tech.tablesaw.api.QuerySupport.numberColumn) Column(tech.tablesaw.columns.Column) CategoricalColumn(tech.tablesaw.api.CategoricalColumn) IntColumn(tech.tablesaw.api.IntColumn) ArrayList(java.util.ArrayList) StandardTableSliceGroup(tech.tablesaw.table.StandardTableSliceGroup) TableSliceGroup(tech.tablesaw.table.TableSliceGroup)

Aggregations

TableSliceGroup (tech.tablesaw.table.TableSliceGroup)3 IntColumn (tech.tablesaw.api.IntColumn)2 Table (tech.tablesaw.api.Table)2 StandardTableSliceGroup (tech.tablesaw.table.StandardTableSliceGroup)2 ArrayList (java.util.ArrayList)1 CategoricalColumn (tech.tablesaw.api.CategoricalColumn)1 ColumnType (tech.tablesaw.api.ColumnType)1 QuerySupport.numberColumn (tech.tablesaw.api.QuerySupport.numberColumn)1 Column (tech.tablesaw.columns.Column)1 TableSlice (tech.tablesaw.table.TableSlice)1