Search in sources :

Example 31 with Table

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

the class Relation method structure.

public Table structure() {
    Table t = Table.create("Structure of " + name());
    IntColumn index = IntColumn.indexColumn("Index", columnCount(), 0);
    StringColumn columnName = StringColumn.create("Column Name", columnCount());
    StringColumn columnType = StringColumn.create("Column Type", columnCount());
    t.addColumns(index);
    t.addColumns(columnName);
    t.addColumns(columnType);
    for (int i = 0; i < columnCount(); i++) {
        Column<?> column = this.columns().get(i);
        columnType.set(i, column.type().name());
        columnName.set(i, columnNames().get(i));
    }
    return t;
}
Also used : StringColumn(tech.tablesaw.api.StringColumn) Table(tech.tablesaw.api.Table) IntColumn(tech.tablesaw.api.IntColumn)

Example 32 with Table

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

the class HtmlReader method read.

@Override
public Table read(HtmlReadOptions options) throws IOException {
    Document doc;
    InputStream inputStream = options.source().inputStream();
    if (inputStream != null) {
        // Reader must support mark, so can't use InputStreamReader
        // Parse the InputStream directly
        doc = Jsoup.parse(inputStream, null, "");
    } else {
        doc = Parser.htmlParser().parseInput(options.source().createReader(null), "");
    }
    Elements tables = doc.select("table");
    int tableIndex = 0;
    if (tables.size() != 1) {
        if (options.tableIndex() != null) {
            if (options.tableIndex() >= 0 && options.tableIndex() < tables.size()) {
                tableIndex = options.tableIndex();
            } else {
                throw new IndexOutOfBoundsException("Table index outside bounds. The URL has " + tables.size() + " tables");
            }
        } else {
            throw new IllegalStateException(tables.size() + " tables found. When more than one html table is present on the page you must specify the index of the table to read from.");
        }
    }
    Element htmlTable = tables.get(tableIndex);
    List<String[]> rows = new ArrayList<>();
    for (Element row : htmlTable.select("tr")) {
        Elements headerCells = row.getElementsByTag("th");
        Elements cells = row.getElementsByTag("td");
        String[] nextLine = Stream.concat(headerCells.stream(), cells.stream()).map(Element::text).toArray(String[]::new);
        rows.add(nextLine);
    }
    Table table = Table.create(options.tableName());
    if (rows.isEmpty()) {
        return table;
    }
    List<String> columnNames = new ArrayList<>();
    if (options.header()) {
        String[] headerRow = rows.remove(0);
        for (int i = 0; i < headerRow.length; i++) {
            columnNames.add(headerRow[i]);
        }
    } else {
        for (int i = 0; i < rows.get(0).length; i++) {
            columnNames.add("C" + i);
        }
    }
    return TableBuildingUtils.build(columnNames, rows, options);
}
Also used : Table(tech.tablesaw.api.Table) InputStream(java.io.InputStream) Element(org.jsoup.nodes.Element) ArrayList(java.util.ArrayList) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements)

Example 33 with Table

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

the class FileReader method parseRows.

protected Table parseRows(ReadOptions options, boolean headerOnly, Reader reader, ReadOptions.ColumnTypeReadOptions columnTypeReadOptions, AbstractParser<?> parser, int sampleSize) {
    parser.beginParsing(reader);
    Table table = Table.create(options.tableName());
    List<String> headerRow = Lists.newArrayList(getColumnNames(options, columnTypeReadOptions, parser));
    @SuppressWarnings({ "UnstableApiUsage", "OptionalGetWithoutIsPresent" }) ColumnType[] types = Streams.mapWithIndex(headerRow.stream(), (columnName, idx) -> columnTypeReadOptions.columnType((int) idx, columnName)).filter(Optional::isPresent).map(Optional::get).toArray(ColumnType[]::new);
    for (int x = 0; x < types.length; x++) {
        if (types[x] != SKIP) {
            String columnName = cleanName(headerRow.get(x));
            if (Strings.isNullOrEmpty(columnName)) {
                columnName = "Column " + table.columnCount();
            }
            Column<?> newColumn = types[x].create(columnName);
            table.addColumns(newColumn);
        }
    }
    if (!headerOnly) {
        String[] columnNames = selectColumnNames(headerRow, types);
        int[] columnIndexes = new int[columnNames.length];
        for (int i = 0; i < columnIndexes.length; i++) {
            // get the index in the original table, which includes skipped fields
            columnIndexes[i] = headerRow.indexOf(columnNames[i]);
        }
        addRows(options, types, parser, table, columnIndexes, sampleSize);
    }
    return table;
}
Also used : Logger(org.slf4j.Logger) ColumnType(tech.tablesaw.api.ColumnType) AbstractParser(com.univocity.parsers.common.AbstractParser) Iterator(java.util.Iterator) Table(tech.tablesaw.api.Table) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Random(java.util.Random) Reader(java.io.Reader) Streams(com.google.common.collect.Streams) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) List(java.util.List) Lists(com.google.common.collect.Lists) AbstractColumnParser(tech.tablesaw.columns.AbstractColumnParser) Map(java.util.Map) Column(tech.tablesaw.columns.Column) SKIP(tech.tablesaw.api.ColumnType.SKIP) Optional(java.util.Optional) NoSuchElementException(java.util.NoSuchElementException) Table(tech.tablesaw.api.Table) ColumnType(tech.tablesaw.api.ColumnType) Optional(java.util.Optional)

Example 34 with Table

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

the class TableSlice method first.

@Override
public Table first(int nRows) {
    int count = 0;
    PrimitiveIterator.OfInt it = sourceRowNumberIterator();
    Table copy = table.emptyCopy();
    while (it.hasNext() && count < nRows) {
        int row = it.nextInt();
        copy.addRow(table.row(row));
        count++;
    }
    return copy;
}
Also used : Table(tech.tablesaw.api.Table) PrimitiveIterator(java.util.PrimitiveIterator)

Example 35 with Table

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

the class DataFrameReader method db.

public Table db(ResultSet resultSet, String tableName) throws SQLException {
    Table table = SqlResultSetReader.read(resultSet);
    table.setName(tableName);
    return table;
}
Also used : Table(tech.tablesaw.api.Table)

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