Java Code Examples for tech.tablesaw.api.Table#rowCount()

The following examples show how to use tech.tablesaw.api.Table#rowCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PivotTable.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static Map<String, Double> getValueMap(
    CategoricalColumn<?> column1,
    CategoricalColumn<?> column2,
    NumericColumn<?> values,
    int valueIndex,
    TableSlice slice,
    AggregateFunction<?, ?> function) {

  Table temp = slice.asTable();
  Table summary = temp.summarize(values.name(), function).by(column1.name(), column2.name());

  Map<String, Double> valueMap = new HashMap<>();
  NumericColumn<?> nc = summary.numberColumn(summary.columnCount() - 1);
  for (int i = 0; i < summary.rowCount(); i++) {
    valueMap.put(String.valueOf(summary.get(i, 1)), nc.getDouble(i));
  }
  return valueMap;
}
 
Example 2
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilter3() {
  Table result =
      table.where(
          table
              .dateColumn("date")
              .isInApril()
              .and(table.numberColumn("approval").isGreaterThan(70)));

  DateColumn dates = result.dateColumn("date");
  ShortColumn approval = result.shortColumn("approval");
  for (int row = 0; row < result.rowCount(); row++) {
    assertTrue(PackedLocalDate.isInApril(dates.getIntInternal(row)));
    assertTrue(approval.get(row) > 70);
  }
}
 
Example 3
Source File: TableSliceGroup.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * For a subtable that is grouped by the values in more than one column, split the grouping column
 * into separate cols and return the revised view
 */
private Table splitGroupingColumn(Table groupTable) {

  if (splitColumnNames.length > 0) {
    List<Column<?>> newColumns = new ArrayList<>();
    List<Column<?>> columns = sourceTable.columns(splitColumnNames);
    for (Column<?> column : columns) {
      Column<?> newColumn = column.emptyCopy();
      newColumns.add(newColumn);
    }
    // iterate through the rows in the table and split each of the grouping columns into multiple
    // columns
    for (int row = 0; row < groupTable.rowCount(); row++) {
      List<String> strings = SPLITTER.splitToList(groupTable.stringColumn("Group").get(row));
      for (int col = 0; col < newColumns.size(); col++) {
        newColumns.get(col).appendCell(strings.get(col));
      }
    }
    for (int col = 0; col < newColumns.size(); col++) {
      Column<?> c = newColumns.get(col);
      groupTable.insertColumn(col, c);
    }
    groupTable.removeColumns("Group");
  }
  return groupTable;
}
 
Example 4
Source File: TableSliceGroup.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/**
 * For a subtable that is grouped by the values in more than one column, split the grouping column
 * into separate cols and return the revised view
 */
private Table splitGroupingColumn(Table groupTable) {

  if (splitColumnNames.length > 0) {
    List<Column<?>> newColumns = new ArrayList<>();
    List<Column<?>> columns = sourceTable.columns(splitColumnNames);
    for (Column<?> column : columns) {
      Column<?> newColumn = column.emptyCopy();
      newColumns.add(newColumn);
    }
    // iterate through the rows in the table and split each of the grouping columns into multiple
    // columns
    for (int row = 0; row < groupTable.rowCount(); row++) {
      List<String> strings = SPLITTER.splitToList(groupTable.stringColumn("Group").get(row));
      for (int col = 0; col < newColumns.size(); col++) {
        newColumns.get(col).appendCell(strings.get(col));
      }
    }
    for (int col = 0; col < newColumns.size(); col++) {
      Column<?> c = newColumns.get(col);
      groupTable.insertColumn(col, c);
    }
    groupTable.removeColumns("Group");
  }
  return groupTable;
}
 
Example 5
Source File: PivotTable.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private static Map<String, Double> getValueMap(
    CategoricalColumn<?> column1,
    CategoricalColumn<?> column2,
    NumericColumn<?> values,
    int valueIndex,
    TableSlice slice,
    AggregateFunction<?, ?> function) {

  Table temp = slice.asTable();
  Table summary = temp.summarize(values.name(), function).by(column1.name(), column2.name());

  Map<String, Double> valueMap = new HashMap<>();
  NumericColumn<?> nc = summary.numberColumn(summary.columnCount() - 1);
  for (int i = 0; i < summary.rowCount(); i++) {
    valueMap.put(String.valueOf(summary.get(i, 1)), nc.getDouble(i));
  }
  return valueMap;
}
 
Example 6
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@Test
public void testFilter3() {
  Table result =
      table.where(
          table
              .dateColumn("date")
              .isInApril()
              .and(table.numberColumn("approval").isGreaterThan(70)));

  DateColumn dates = result.dateColumn("date");
  ShortColumn approval = result.shortColumn("approval");
  for (int row = 0; row < result.rowCount(); row++) {
    assertTrue(PackedLocalDate.isInApril(dates.getIntInternal(row)));
    assertTrue(approval.get(row) > 70);
  }
}
 
Example 7
Source File: TableAssertions.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
/** Make sure each row in each table match */
public static void assertTableEquals(Table expected, Table actual) {
  assertEquals(actual.rowCount(), expected.rowCount(), "tables should have same number of rows");
  assertEquals(
      actual.columnCount(), expected.columnCount(), "tables should have same number of columns");
  int maxRows = actual.rowCount();
  int numberOfColumns = actual.columnCount();
  for (int rowIndex = 0; rowIndex < maxRows; rowIndex++) {
    for (int columnIndex = 0; columnIndex < numberOfColumns; columnIndex++) {
      assertEquals(
          actual.get(rowIndex, columnIndex),
          expected.get(rowIndex, columnIndex),
          "cells[" + rowIndex + ", " + columnIndex + "] do not match");
    }
  }
}
 
Example 8
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectRange() {
  Table result = table.inRange(20, 30);
  assertEquals(10, result.rowCount());
  for (Column<?> c : result.columns()) {
    for (int r = 0; r < result.rowCount(); r++) {
      assertEquals(table.getString(r + 20, c.name()), result.getString(r, c.name()));
    }
  }
}
 
Example 9
Source File: SelectionTableSliceGroup.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private SelectionTableSliceGroup(Table original, String subTableNameTemplate, int step) {
  super(original);
  List<Selection> selections = new ArrayList<>();
  for (int i = 0; i < original.rowCount() - step; i += step) {
    Selection selection = new BitmapBackedSelection();
    selection.addRange(i, i + step);
    selections.add(selection);
  }
  splitOnSelection(subTableNameTemplate, selections);
}
 
Example 10
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static void tail(int rowsToInclude, Table oldTable, Table newTable) {
  int oldTableSize = oldTable.rowCount();
  int start = oldTableSize - rowsToInclude;
  Selection rows = new BitmapBackedSelection(rowsToInclude);
  for (int i = start; i < oldTableSize; i++) {
    rows.add(i);
  }
  copyRowsToTable(rows, oldTable, newTable);
}
 
Example 11
Source File: HtmlWriter.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public void write(Table table, HtmlWriteOptions options) throws IOException {
  ElementCreator elements = options.elementCreator();
  Element html = elements.create("table");
  html.appendChild(header(table.columns(), elements));

  Element tbody = elements.create("tbody");
  html.appendChild(tbody);
  for (int row = 0; row < table.rowCount(); row++) {
    tbody.appendChild(row(row, table, elements, options));
  }

  try (Writer writer = options.destination().createWriter()) {
    writer.write(html.toString());
  }
}
 
Example 12
Source File: DataFrameJoinerPerformanceTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private static Table addFillerColumn(Table table, int numberColumnsToAdd, String prefix) {
  int[] filler = new int[table.rowCount()];
  Arrays.fill(filler, 1);
  IntColumn col = IntColumn.create("temp", filler);
  for (int i = 0; i < numberColumnsToAdd; i++) {
    table.addColumns(col.copy().setName(prefix + "_appendColumn" + i));
  }
  return table;
}
 
Example 13
Source File: CrossTabTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testCounts1() throws Exception {
  Table bush = Table.read().csv("../data/bush.csv");
  Table counts = CrossTab.counts(bush, "who");
  Table pcts = CrossTab.percents(bush, "who");
  double sum = counts.numberColumn("Count").sum();
  for (int row = 0; row < pcts.rowCount(); row++) {
    assertEquals(counts.intColumn("Count").get(row) / sum, pcts.doubleColumn(1).get(row), 0.01);
  }
}
 
Example 14
Source File: FileReader.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
protected String getTypeString(Table structure) {
  StringBuilder buf = new StringBuilder();
  buf.append("ColumnType[] columnTypes = {");
  buf.append(System.lineSeparator());

  Column<?> typeCol = structure.column("Column Type");
  Column<?> indxCol = structure.column("Index");
  Column<?> nameCol = structure.column("Column Name");

  // add the column headers
  int typeColIndex = structure.columnIndex(typeCol);
  int indxColIndex = structure.columnIndex(indxCol);
  int nameColIndex = structure.columnIndex(nameCol);

  int typeColWidth = typeCol.columnWidth();
  int indxColWidth = indxCol.columnWidth();
  int nameColWidth = nameCol.columnWidth();

  final char padChar = ' ';
  for (int r = 0; r < structure.rowCount(); r++) {
    String cell = Strings.padEnd(structure.get(r, typeColIndex) + ",", typeColWidth, padChar);
    buf.append(cell);
    buf.append(" // ");

    cell = Strings.padEnd(structure.getUnformatted(r, indxColIndex), indxColWidth, padChar);
    buf.append(cell);
    buf.append(' ');

    cell = Strings.padEnd(structure.getUnformatted(r, nameColIndex), nameColWidth, padChar);
    buf.append(cell);
    buf.append(' ');

    buf.append(System.lineSeparator());
  }
  buf.append("}");
  buf.append(System.lineSeparator());
  return buf.toString();
}
 
Example 15
Source File: CrossTabTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testCounts2() throws Exception {
  Table bush = Table.read().csv("../data/bush.csv");
  Table counts = CrossTab.counts(bush, "date");
  Table pcts = CrossTab.percents(bush, "date");
  double sum = counts.numberColumn("Count").sum();
  for (int row = 0; row < pcts.rowCount(); row++) {
    assertEquals(counts.intColumn("Count").get(row) / sum, pcts.doubleColumn(1).get(row), 0.01);
  }
}
 
Example 16
Source File: CrossTabTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testCounts1() throws Exception {
  Table bush = Table.read().csv("../data/bush.csv");
  Table counts = CrossTab.counts(bush, "who");
  Table pcts = CrossTab.percents(bush, "who");
  double sum = counts.numberColumn("Count").sum();
  for (int row = 0; row < pcts.rowCount(); row++) {
    assertEquals(counts.intColumn("Count").get(row) / sum, pcts.doubleColumn(1).get(row), 0.01);
  }
}
 
Example 17
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testRejectRange() {
  Table result = table.dropRange(20, 30);
  assertEquals(table.rowCount() - 10, result.rowCount());
  for (Column<?> c : result.columns()) {
    for (int r = 30; r < result.rowCount(); r++) {
      assertEquals(result.getString(r, c.name()), table.getString(r + 10, c.name()));
    }
  }
}
 
Example 18
Source File: CrossTabTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testCounts2() throws Exception {
  Table bush = Table.read().csv("../data/bush.csv");
  Table counts = CrossTab.counts(bush, "date");
  Table pcts = CrossTab.percents(bush, "date");
  double sum = counts.numberColumn("Count").sum();
  for (int row = 0; row < pcts.rowCount(); row++) {
    assertEquals(counts.intColumn("Count").get(row) / sum, pcts.doubleColumn(1).get(row), 0.01);
  }
}
 
Example 19
Source File: FileReader.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
private void addRows(
    ReadOptions options,
    ColumnType[] types,
    AbstractParser<?> reader,
    Table table,
    int[] columnIndexes,
    int sampleSize) {

  String[] nextLine;
  Map<String, AbstractColumnParser<?>> parserMap = getParserMap(options, table);

  Random random = new Random(0);
  // Add the rows
  for (int rowNumber = options.header() ? 1 : 0;
      (nextLine = reader.parseNext()) != null;
      rowNumber++) {
    // validation
    if (nextLine.length < types.length) {
      if (nextLine.length == 1 && Strings.isNullOrEmpty(nextLine[0])) {
        logger.error("Warning: Invalid file. Row " + rowNumber + " is empty. Continuing.");
        continue;
      } else {
        Exception e =
            new IndexOutOfBoundsException(
                "Row number "
                    + rowNumber
                    + " contains "
                    + nextLine.length
                    + " columns. "
                    + types.length
                    + " expected.");
        throw new AddCellToColumnException(e, 0, rowNumber, table.columnNames(), nextLine);
      }
    } else if (nextLine.length > types.length) {
      throw new IllegalArgumentException(
          "Row number "
              + rowNumber
              + " contains "
              + nextLine.length
              + " columns. "
              + types.length
              + " expected.");
    }

    int samplesCount = table.rowCount();
    if (sampleSize < 0 || samplesCount < sampleSize) {
      addValuesToColumns(table, columnIndexes, nextLine, parserMap, rowNumber, -1);
    } else {
      // find a row index to replace
      int randomIndex = random.nextInt(samplesCount + 1);
      // replace index if it is smaller than numSamples, otherwise ignore it.
      if (randomIndex < sampleSize) {
        addValuesToColumns(table, columnIndexes, nextLine, parserMap, rowNumber, randomIndex);
      }
    }
  }
}
 
Example 20
Source File: FileReader.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
private void addRows(
    ReadOptions options,
    ColumnType[] types,
    AbstractParser<?> reader,
    Table table,
    int[] columnIndexes,
    int sampleSize) {

  String[] nextLine;
  Map<String, AbstractColumnParser<?>> parserMap = getParserMap(options, table);

  Random random = new Random(0);
  // Add the rows
  for (int rowNumber = options.header() ? 1 : 0;
      (nextLine = reader.parseNext()) != null;
      rowNumber++) {
    // validation
    if (nextLine.length < types.length) {
      if (nextLine.length == 1 && Strings.isNullOrEmpty(nextLine[0])) {
        logger.error("Warning: Invalid file. Row " + rowNumber + " is empty. Continuing.");
        continue;
      } else {
        Exception e =
            new IndexOutOfBoundsException(
                "Row number "
                    + rowNumber
                    + " contains "
                    + nextLine.length
                    + " columns. "
                    + types.length
                    + " expected.");
        throw new AddCellToColumnException(e, 0, rowNumber, table.columnNames(), nextLine);
      }
    } else if (nextLine.length > types.length) {
      throw new IllegalArgumentException(
          "Row number "
              + rowNumber
              + " contains "
              + nextLine.length
              + " columns. "
              + types.length
              + " expected.");
    }

    int samplesCount = table.rowCount();
    if (sampleSize < 0 || samplesCount < sampleSize) {
      addValuesToColumns(table, columnIndexes, nextLine, parserMap, rowNumber, -1);
    } else {
      // find a row index to replace
      int randomIndex = random.nextInt(samplesCount + 1);
      // replace index if it is smaller than numSamples, otherwise ignore it.
      if (randomIndex < sampleSize) {
        addValuesToColumns(table, columnIndexes, nextLine, parserMap, rowNumber, randomIndex);
      }
    }
  }
}