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

The following examples show how to use tech.tablesaw.api.Table#column() . 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: DataFrameJoiner.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private void crossProduct(
    Table destination,
    Table table1,
    Table table2,
    Selection table1Rows,
    Selection table2Rows,
    Set<Integer> ignoreColumns) {
  for (int c = 0; c < table1.columnCount() + table2.columnCount(); c++) {
    if (ignoreColumns.contains(c)) {
      continue;
    }
    int table2Index = c - table1.columnCount();
    for (int r1 : table1Rows) {
      for (int r2 : table2Rows) {
        if (c < table1.columnCount()) {
          Column t1Col = table1.column(c);
          destination.column(c).append(t1Col, r1);
        } else {
          Column t2Col = table2.column(table2Index);
          destination.column(c).append(t2Col, r2);
        }
      }
    }
  }
}
 
Example 2
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private void crossProduct(
    Table destination,
    Table table1,
    Table table2,
    Selection table1Rows,
    Selection table2Rows,
    Set<Integer> ignoreColumns) {
  for (int c = 0; c < table1.columnCount() + table2.columnCount(); c++) {
    if (ignoreColumns.contains(c)) {
      continue;
    }
    int table2Index = c - table1.columnCount();
    for (int r1 : table1Rows) {
      for (int r2 : table2Rows) {
        if (c < table1.columnCount()) {
          Column t1Col = table1.column(c);
          destination.column(c).append(t1Col, r1);
        } else {
          Column t2Col = table2.column(table2Index);
          destination.column(c).append(t2Col, r2);
        }
      }
    }
  }
}
 
Example 3
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
private void withMissingLeftJoin(
    Table destination, Table table1, Selection table1Rows, Set<Integer> ignoreColumns) {
  for (int c = 0; c < destination.columnCount(); c++) {
    if (ignoreColumns.contains(c)) {
      continue;
    }
    if (c < table1.columnCount()) {
      Column t1Col = table1.column(c);
      for (int index : table1Rows) {
        destination.column(c).append(t1Col, index);
      }
    } else {
      for (int r1 = 0; r1 < table1Rows.size(); r1++) {
        destination.column(c).appendMissing();
      }
    }
  }
}
 
Example 4
Source File: FileReader.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
private void addValuesToColumns(
    Table table,
    int[] columnIndexes,
    String[] nextLine,
    Map<String, AbstractColumnParser<?>> parserMap,
    int rowNumber,
    int rowIndex) {
  // append each column that we're including (not skipping)
  int cellIndex = 0;
  for (int columnIndex : columnIndexes) {
    Column<?> column = table.column(cellIndex);
    AbstractColumnParser<?> parser = parserMap.get(column.name());
    try {
      String value = nextLine[columnIndex];
      if (rowIndex >= 0) {
        column.set(rowIndex, value, parser);
      } else {
        column.appendCell(value, parser);
      }
    } catch (Exception e) {
      throw new AddCellToColumnException(
          e, columnIndex, rowNumber, table.columnNames(), nextLine);
    }
    cellIndex++;
  }
}
 
Example 5
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 6
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Create a big multicolumn selection for all join columns in the given table. */
private Selection createMultiColSelection(
    Table table1, int ri, List<Index> indexes, int selectionSize) {
  Selection multiColSelection = Selection.withRange(0, selectionSize);
  int i = 0;
  for (Integer joinColumnIndex : joinColumnIndexes) {
    Column<?> col = table1.column(joinColumnIndex);
    Selection oneColSelection = selectionForColumn(col, ri, indexes.get(i));
    // and the selections.
    multiColSelection = multiColSelection.and(oneColSelection);
    i++;
  }
  return multiColSelection;
}
 
Example 7
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
public static void appendRowToTable(int row, Table oldTable, Table newTable) {
  int[] rows = new int[] {row};
  for (int columnIndex = 0; columnIndex < oldTable.columnCount(); columnIndex++) {
    Column oldColumn = oldTable.column(columnIndex);
    for (int i : rows) {
      newTable.column(columnIndex).append(oldColumn, i);
    }
  }
}
 
Example 8
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Copies the rows indicated by the row index values in the given array from oldTable to newTable
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public static void copyRowsToTable(int[] rows, Table oldTable, Table newTable) {
  for (int columnIndex = 0; columnIndex < oldTable.columnCount(); columnIndex++) {
    Column oldColumn = oldTable.column(columnIndex);
    int r = 0;
    for (int i : rows) {
      newTable.column(columnIndex).set(r, oldColumn, i);
      r++;
    }
  }
}
 
Example 9
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Copies the rows indicated by the row index values in the given selection from oldTable to
 * newTable
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public static void copyRowsToTable(Selection rows, Table oldTable, Table newTable) {
  for (int columnIndex = 0; columnIndex < oldTable.columnCount(); columnIndex++) {
    Column oldColumn = oldTable.column(columnIndex);
    int r = 0;
    for (int i : rows) {
      newTable.column(columnIndex).set(r, oldColumn, i);
      r++;
    }
  }
}
 
Example 10
Source File: SortUtils.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a comparator that can be used to sort the records in this table according to the given
 * sort key
 */
public static IntComparator getComparator(Table table, Sort key) {
  Iterator<Map.Entry<String, Sort.Order>> entries = key.iterator();
  Map.Entry<String, Sort.Order> sort = entries.next();
  Column<?> column = table.column(sort.getKey());
  return SortUtils.rowComparator(column, sort.getValue());
}
 
Example 11
Source File: SortUtils.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Returns a comparator chain for sorting according to the given key */
public static IntComparatorChain getChain(Table table, Sort key) {
  Iterator<Map.Entry<String, Sort.Order>> entries = key.iterator();
  Map.Entry<String, Sort.Order> sort = entries.next();
  Column<?> column = table.column(sort.getKey());
  IntComparator comparator = rowComparator(column, sort.getValue());

  IntComparatorChain chain = new IntComparatorChain(comparator);
  while (entries.hasNext()) {
    sort = entries.next();
    chain.addComparator(rowComparator(table.column(sort.getKey()), sort.getValue()));
  }
  return chain;
}
 
Example 12
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Create a big multicolumn selection for all join columns in the given table. */
private Selection createMultiColSelection(
    Table table1, int ri, List<Index> indexes, int selectionSize) {
  Selection multiColSelection = Selection.withRange(0, selectionSize);
  int i = 0;
  for (Integer joinColumnIndex : joinColumnIndexes) {
    Column<?> col = table1.column(joinColumnIndex);
    Selection oneColSelection = selectionForColumn(col, ri, indexes.get(i));
    // and the selections.
    multiColSelection = multiColSelection.and(oneColSelection);
    i++;
  }
  return multiColSelection;
}
 
Example 13
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes", "unchecked"})
public static void appendRowToTable(int row, Table oldTable, Table newTable) {
  int[] rows = new int[] {row};
  for (int columnIndex = 0; columnIndex < oldTable.columnCount(); columnIndex++) {
    Column oldColumn = oldTable.column(columnIndex);
    for (int i : rows) {
      newTable.column(columnIndex).append(oldColumn, i);
    }
  }
}
 
Example 14
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Copies the rows indicated by the row index values in the given array from oldTable to newTable
 */
@SuppressWarnings({"rawtypes", "unchecked"})
public static void copyRowsToTable(int[] rows, Table oldTable, Table newTable) {
  for (int columnIndex = 0; columnIndex < oldTable.columnCount(); columnIndex++) {
    Column oldColumn = oldTable.column(columnIndex);
    int r = 0;
    for (int i : rows) {
      newTable.column(columnIndex).set(r, oldColumn, i);
      r++;
    }
  }
}
 
Example 15
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 16
Source File: SortUtils.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a comparator that can be used to sort the records in this table according to the given
 * sort key
 */
public static IntComparator getComparator(Table table, Sort key) {
  Iterator<Map.Entry<String, Sort.Order>> entries = key.iterator();
  Map.Entry<String, Sort.Order> sort = entries.next();
  Column<?> column = table.column(sort.getKey());
  return SortUtils.rowComparator(column, sort.getValue());
}
 
Example 17
Source File: SortUtils.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
/** Returns a comparator chain for sorting according to the given key */
public static IntComparatorChain getChain(Table table, Sort key) {
  Iterator<Map.Entry<String, Sort.Order>> entries = key.iterator();
  Map.Entry<String, Sort.Order> sort = entries.next();
  Column<?> column = table.column(sort.getKey());
  IntComparator comparator = rowComparator(column, sort.getValue());

  IntComparatorChain chain = new IntComparatorChain(comparator);
  while (entries.hasNext()) {
    sort = entries.next();
    chain.addComparator(rowComparator(table.column(sort.getKey()), sort.getValue()));
  }
  return chain;
}
 
Example 18
Source File: SqlResultSetReader.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new table with the given tableName, constructed from the given result set
 *
 * @throws SQLException if there is a problem detected in the database
 */
public static Table read(ResultSet resultSet) throws SQLException {

  ResultSetMetaData metaData = resultSet.getMetaData();
  Table table = Table.create();

  // Setup the columns and add to the table
  for (int i = 1; i <= metaData.getColumnCount(); i++) {
    ColumnType type =
        getColumnType(metaData.getColumnType(i), metaData.getScale(i), metaData.getPrecision(i));

    Preconditions.checkState(
        type != null,
        "No column type found for %s as specified for column %s",
        metaData.getColumnType(i),
        metaData.getColumnName(i));

    Column<?> newColumn = type.create(metaData.getColumnName(i));
    table.addColumns(newColumn);
  }

  // Add the rows
  while (resultSet.next()) {
    for (int i = 1; i <= metaData.getColumnCount(); i++) {
      Column<?> column =
          table.column(i - 1); // subtract 1 because results sets originate at 1 not 0
      if (column instanceof ShortColumn) {
        appendToColumn(column, resultSet, resultSet.getShort(i));
      } else if (column instanceof IntColumn) {
        appendToColumn(column, resultSet, resultSet.getInt(i));
      } else if (column instanceof LongColumn) {
        appendToColumn(column, resultSet, resultSet.getLong(i));
      } else if (column instanceof FloatColumn) {
        appendToColumn(column, resultSet, resultSet.getFloat(i));
      } else if (column instanceof DoubleColumn) {
        appendToColumn(column, resultSet, resultSet.getDouble(i));
      } else if (column instanceof BooleanColumn) {
        appendToColumn(column, resultSet, resultSet.getBoolean(i));
      } else {
        column.appendObj(resultSet.getObject(i));
      }
    }
  }
  return table;
}
 
Example 19
Source File: SqlResultSetReader.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new table with the given tableName, constructed from the given result set
 *
 * @throws SQLException if there is a problem detected in the database
 */
public static Table read(ResultSet resultSet) throws SQLException {

  ResultSetMetaData metaData = resultSet.getMetaData();
  Table table = Table.create();

  // Setup the columns and add to the table
  for (int i = 1; i <= metaData.getColumnCount(); i++) {
    ColumnType type =
        getColumnType(metaData.getColumnType(i), metaData.getScale(i), metaData.getPrecision(i));

    Preconditions.checkState(
        type != null,
        "No column type found for %s as specified for column %s",
        metaData.getColumnType(i),
        metaData.getColumnName(i));

    Column<?> newColumn = type.create(metaData.getColumnName(i));
    table.addColumns(newColumn);
  }

  // Add the rows
  while (resultSet.next()) {
    for (int i = 1; i <= metaData.getColumnCount(); i++) {
      Column<?> column =
          table.column(i - 1); // subtract 1 because results sets originate at 1 not 0
      if (column instanceof ShortColumn) {
        appendToColumn(column, resultSet, resultSet.getShort(i));
      } else if (column instanceof IntColumn) {
        appendToColumn(column, resultSet, resultSet.getInt(i));
      } else if (column instanceof LongColumn) {
        appendToColumn(column, resultSet, resultSet.getLong(i));
      } else if (column instanceof FloatColumn) {
        appendToColumn(column, resultSet, resultSet.getFloat(i));
      } else if (column instanceof DoubleColumn) {
        appendToColumn(column, resultSet, resultSet.getDouble(i));
      } else if (column instanceof BooleanColumn) {
        appendToColumn(column, resultSet, resultSet.getBoolean(i));
      } else {
        column.appendObj(resultSet.getObject(i));
      }
    }
  }
  return table;
}
 
Example 20
Source File: GettingStarted.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
private void workingWithTablesColumns() {

    // Setup code.
    Table table = Table.create();
    StringColumn column1 = StringColumn.create("col1");
    StringColumn column2 = StringColumn.create("col2");
    StringColumn column3 = StringColumn.create("col3");

    try {
      // @@ working_with_columns_examples
      List<String> columnNames = table.columnNames(); // returns all column names
      List<Column<?>> columns = table.columns(); // returns all the columns in the table

      // removing columns
      table.removeColumns("Foo"); // keep everything but "foo"
      table.retainColumns("Foo", "Bar"); // only keep foo and bar
      table.removeColumnsWithMissingValues();

      // adding columns
      table.addColumns(column1, column2, column3);
      // @@ working_with_columns_examples

      // @@ get_column_case_insensitive
      table.column("FOO");
      table.column("foo");
      table.column("foO");
      // @@ get_column_case_insensitive

      // @@ get_column
      table.column("Foo"); // returns the column named 'Foo' if it's in the table.
      // or
      table.column(0); // returns the first column
      // @@ get_column

      // @@ get_column_cast
      StringColumn sc = (StringColumn) table.column(0);
      // @@ get_column_cast

      // @@ get_typed_columns
      StringColumn strings = table.stringColumn(0);
      DateColumn dates = table.dateColumn("start date");
      DoubleColumn doubles = table.doubleColumn("doubles");
      // @@ get_typed_columns

    } catch (Exception e) {
    }
  }