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

The following examples show how to use tech.tablesaw.api.Table#columnCount() . 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: 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 2
Source File: TableBuildingUtils.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static Table build(
    List<String> columnNames, List<String[]> dataRows, ReadOptions options) {
  Table table = Table.create(options.tableName());

  if (dataRows.isEmpty()) {
    return table;
  }

  ColumnTypeDetector detector = new ColumnTypeDetector(options.columnTypesToDetect());
  Iterator<String[]> iterator = dataRows.iterator();
  ColumnType[] types = detector.detectColumnTypes(iterator, options);
  for (int i = 0; i < columnNames.size(); i++) {
    table.addColumns(types[i].create(columnNames.get(i)));
  }

  for (int i = 0; i < dataRows.size(); i++) {
    for (int j = 0; j < table.columnCount(); j++) {
      table.column(j).appendCell(dataRows.get(i)[j]);
    }
  }

  return table;
}
 
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: 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 5
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 6
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 7
Source File: TableBuildingUtils.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
public static Table build(
    List<String> columnNames, List<String[]> dataRows, ReadOptions options) {
  Table table = Table.create(options.tableName());

  if (dataRows.isEmpty()) {
    return table;
  }

  ColumnTypeDetector detector = new ColumnTypeDetector(options.columnTypesToDetect());
  Iterator<String[]> iterator = dataRows.iterator();
  ColumnType[] types = detector.detectColumnTypes(iterator, options);
  for (int i = 0; i < columnNames.size(); i++) {
    table.addColumns(types[i].create(columnNames.get(i)));
  }

  for (int i = 0; i < dataRows.size(); i++) {
    for (int j = 0; j < table.columnCount(); j++) {
      table.column(j).appendCell(dataRows.get(i)[j]);
    }
  }

  return table;
}
 
Example 8
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 9
Source File: ASTDataset.java    From symja_android_library with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Return the value associated to the <code>key</code>. If no value is available return the
 * <code>defaultValue</code>
 * 
 * @param key
 * @param defaultValue
 * @return
 */
public IExpr getValue(IExpr key, Supplier<IExpr> defaultValue) {
	final String keyName = key.toString();
	if (fTable.rowCount() == 1) {
		int columnIndex = fTable.columnIndex(keyName);
		if (columnIndex < 0) {
			return defaultValue.get();
		}
		return select(1, columnIndex + 1);
	}
	String[] strList = new String[] { keyName };
	Table table = fTable.select(strList);
	if (table.columnCount() == 0) {
		return defaultValue.get();
	}
	return newInstance(table);
}
 
Example 10
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 11
Source File: FileReader.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
protected Table parseRows(
    ReadOptions options,
    boolean headerOnly,
    Reader reader,
    ColumnType[] types,
    AbstractParser<?> parser,
    int sampleSize) {
  parser.beginParsing(reader);
  Table table = Table.create(options.tableName());

  List<String> headerRow = Lists.newArrayList(getColumnNames(options, types, parser));

  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;
}
 
Example 12
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static boolean compareRows(int rowInOriginal, Table original, Table tempTable) {
  int columnCount = original.columnCount();
  boolean result;
  for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
    ColumnType columnType = original.column(columnIndex).type();
    result =
        columnType.compare(
            rowInOriginal, tempTable.column(columnIndex), original.column(columnIndex));
    if (!result) {
      return false;
    }
  }
  return true;
}
 
Example 13
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 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 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 15
Source File: FileReader.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
protected Table parseRows(
    ReadOptions options,
    boolean headerOnly,
    Reader reader,
    ColumnType[] types,
    AbstractParser<?> parser,
    int sampleSize) {
  parser.beginParsing(reader);
  Table table = Table.create(options.tableName());

  List<String> headerRow = Lists.newArrayList(getColumnNames(options, types, parser));

  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;
}
 
Example 16
Source File: Rows.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static boolean compareRows(int rowInOriginal, Table original, Table tempTable) {
  int columnCount = original.columnCount();
  boolean result;
  for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
    ColumnType columnType = original.column(columnIndex).type();
    result =
        columnType.compare(
            rowInOriginal, tempTable.column(columnIndex), original.column(columnIndex));
    if (!result) {
      return false;
    }
  }
  return true;
}
 
Example 17
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 18
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 19
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
/**
 * Adds empty columns to the destination table with the same type as columns in table1 and table2.
 *
 * <p>For inner, left and full outer join types the join columns in table2 are not needed and will
 * be marked as placeholders. The indexes of those columns will be returned. The downstream logic
 * is easier if we wait to remove the redundant columns until the last step.
 *
 * @param destination the table to fill up with columns. Will be mutated in place.
 * @param table1 the table on left side of the join.
 * @param table2 the table on the right side of the join.
 * @param joinType the type of join.
 * @param allowDuplicates whether to allow duplicates. If yes rename columns in table2 that have
 *     the same name as columns in table1 with the exception of join columns in table2 when
 *     performing a right join.
 * @param table2JoinColumnIndexes the index locations of the table2 join columns.
 * @return A
 */
private Set<Integer> emptyTableFromColumns(
    Table destination,
    Table table1,
    Table table2,
    JoinType joinType,
    boolean allowDuplicates,
    List<Integer> table2JoinColumnIndexes) {

  Column<?>[] cols =
      Streams.concat(table1.columns().stream(), table2.columns().stream())
          .map(Column::emptyCopy)
          .toArray(Column[]::new);

  // For inner join, left join and full outer join mark the join columns in table2 as
  // placeholders.
  // For right join mark the join columns in table1 as placeholders.
  // Keep track of which join columns are placeholders so they can be ignored.
  Set<Integer> ignoreColumns = new HashSet<>();
  for (int c = 0; c < cols.length; c++) {
    if (joinType == JoinType.RIGHT_OUTER) {
      if (c < table1.columnCount() && joinColumnIndexes.contains(c)) {
        cols[c].setName("Placeholder_" + ignoreColumns.size());
        ignoreColumns.add(c);
      }
    } else {
      int table2Index = c - table1.columnCount();
      if (c >= table1.columnCount() && table2JoinColumnIndexes.contains(table2Index)) {
        cols[c].setName("Placeholder_" + ignoreColumns.size());
        ignoreColumns.add(c);
      }
    }
  }

  // Rename duplicate columns in second table
  if (allowDuplicates) {
    Set<String> table1ColNames =
        Arrays.stream(cols)
            .map(Column::name)
            .map(String::toLowerCase)
            .limit(table1.columnCount())
            .collect(Collectors.toSet());

    String table2Alias = TABLE_ALIAS + joinTableId.getAndIncrement();
    for (int c = table1.columnCount(); c < cols.length; c++) {
      String columnName = cols[c].name();
      if (table1ColNames.contains(columnName.toLowerCase())) {
        cols[c].setName(newName(table2Alias, columnName));
      }
    }
  }
  destination.addColumns(cols);
  return ignoreColumns;
}
 
Example 20
Source File: DataFrameJoiner.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
/**
 * Adds empty columns to the destination table with the same type as columns in table1 and table2.
 *
 * <p>For inner, left and full outer join types the join columns in table2 are not needed and will
 * be marked as placeholders. The indexes of those columns will be returned. The downstream logic
 * is easier if we wait to remove the redundant columns until the last step.
 *
 * @param destination the table to fill up with columns. Will be mutated in place.
 * @param table1 the table on left side of the join.
 * @param table2 the table on the right side of the join.
 * @param joinType the type of join.
 * @param allowDuplicates whether to allow duplicates. If yes rename columns in table2 that have
 *     the same name as columns in table1 with the exception of join columns in table2 when
 *     performing a right join.
 * @param table2JoinColumnIndexes the index locations of the table2 join columns.
 * @return A
 */
private Set<Integer> emptyTableFromColumns(
    Table destination,
    Table table1,
    Table table2,
    JoinType joinType,
    boolean allowDuplicates,
    List<Integer> table2JoinColumnIndexes) {

  Column<?>[] cols =
      Streams.concat(table1.columns().stream(), table2.columns().stream())
          .map(Column::emptyCopy)
          .toArray(Column[]::new);

  // For inner join, left join and full outer join mark the join columns in table2 as
  // placeholders.
  // For right join mark the join columns in table1 as placeholders.
  // Keep track of which join columns are placeholders so they can be ignored.
  Set<Integer> ignoreColumns = new HashSet<>();
  for (int c = 0; c < cols.length; c++) {
    if (joinType == JoinType.RIGHT_OUTER) {
      if (c < table1.columnCount() && joinColumnIndexes.contains(c)) {
        cols[c].setName("Placeholder_" + ignoreColumns.size());
        ignoreColumns.add(c);
      }
    } else {
      int table2Index = c - table1.columnCount();
      if (c >= table1.columnCount() && table2JoinColumnIndexes.contains(table2Index)) {
        cols[c].setName("Placeholder_" + ignoreColumns.size());
        ignoreColumns.add(c);
      }
    }
  }

  // Rename duplicate columns in second table
  if (allowDuplicates) {
    Set<String> table1ColNames =
        Arrays.stream(cols)
            .map(Column::name)
            .map(String::toLowerCase)
            .limit(table1.columnCount())
            .collect(Collectors.toSet());

    String table2Alias = TABLE_ALIAS + joinTableId.getAndIncrement();
    for (int c = table1.columnCount(); c < cols.length; c++) {
      String columnName = cols[c].name();
      if (table1ColNames.contains(columnName.toLowerCase())) {
        cols[c].setName(newName(table2Alias, columnName));
      }
    }
  }
  destination.addColumns(cols);
  return ignoreColumns;
}