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

The following examples show how to use tech.tablesaw.api.Table#columns() . 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: XlsxReaderTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Table read1(String name, int size, String... columnNames) {
  Table table = readN(name, 1).get(0);
  int colNum = 0;
  for (final Column<?> column : table.columns()) {
    assertEquals(columnNames[colNum], column.name(), "Wrong column name");
    assertEquals(size, column.size(), "Wrong size for column " + columnNames[colNum]);
    colNum++;
  }
  return table;
}
 
Example 2
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 3
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testRejectRows() {
  Table result = table.dropRows(20, 30);
  assertEquals(table.rowCount() - 2, result.rowCount());
  for (Column<?> c : result.columns()) {
    assertEquals(table.getString(21, c.name()), result.getString(20, c.name()));
    assertEquals(table.getString(32, c.name()), result.getString(30, c.name()));
  }
}
 
Example 4
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectRows() {
  Table result = table.rows(20, 30);
  assertEquals(2, result.rowCount());
  for (Column<?> c : result.columns()) {
    assertEquals(table.getString(20, c.name()), result.getString(0, c.name()));
    assertEquals(table.getString(30, c.name()), result.getString(1, c.name()));
  }
}
 
Example 5
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 6
Source File: FileReader.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Map<String, AbstractColumnParser<?>> getParserMap(ReadOptions options, Table table) {
  Map<String, AbstractColumnParser<?>> parserMap = new HashMap<>();
  for (Column<?> column : table.columns()) {
    AbstractColumnParser<?> parser = column.type().customParser(options);
    parserMap.put(column.name(), parser);
  }
  return parserMap;
}
 
Example 7
Source File: TableSliceGroup.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private boolean containsAnyTextColumns(Table original) {
  for (Column<?> column : original.columns()) {
    if (column.type().equals(ColumnType.TEXT)) {
      return true;
    }
  }
  return false;
}
 
Example 8
Source File: Summarizer.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Table combineTables(List<Table> tables) {
  Preconditions.checkArgument(!tables.isEmpty());

  Table result = tables.get(0);
  for (int i = 1; i < tables.size(); i++) {
    Table table = tables.get(i);
    for (Column<?> column : table.columns()) {
      if (tableDoesNotContain(column.name(), result)) {
        result.addColumns(column);
      }
    }
  }
  return result;
}
 
Example 9
Source File: Heatmap.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static Figure create(String title, Table table, String categoryCol1, String categoryCol2) {
  Layout layout = Layout.builder(title).build();

  Table counts = table.xTabCounts(categoryCol1, categoryCol2);
  counts = counts.dropRows(counts.rowCount() - 1);
  List<Column<?>> columns = counts.columns();
  columns.remove(counts.columnCount() - 1);
  Column<?> yColumn = columns.remove(0);
  double[][] z = DoubleArrays.to2dArray(counts.numericColumns());

  Object[] x = counts.columnNames().toArray();
  Object[] y = yColumn.asObjectArray();
  HeatmapTrace trace = HeatmapTrace.builder(x, y, z).build();
  return new Figure(layout, trace);
}
 
Example 10
Source File: XlsxReaderTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Table read1(String name, int size, String... columnNames) {
  Table table = readN(name, 1).get(0);
  int colNum = 0;
  for (final Column<?> column : table.columns()) {
    assertEquals(columnNames[colNum], column.name(), "Wrong column name");
    assertEquals(size, column.size(), "Wrong size for column " + columnNames[colNum]);
    colNum++;
  }
  return table;
}
 
Example 11
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 12
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testRejectRows() {
  Table result = table.dropRows(20, 30);
  assertEquals(table.rowCount() - 2, result.rowCount());
  for (Column<?> c : result.columns()) {
    assertEquals(table.getString(21, c.name()), result.getString(20, c.name()));
    assertEquals(table.getString(32, c.name()), result.getString(30, c.name()));
  }
}
 
Example 13
Source File: TableFilteringTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testSelectRows() {
  Table result = table.rows(20, 30);
  assertEquals(2, result.rowCount());
  for (Column<?> c : result.columns()) {
    assertEquals(table.getString(20, c.name()), result.getString(0, c.name()));
    assertEquals(table.getString(30, c.name()), result.getString(1, c.name()));
  }
}
 
Example 14
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 15
Source File: FileReader.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Map<String, AbstractColumnParser<?>> getParserMap(ReadOptions options, Table table) {
  Map<String, AbstractColumnParser<?>> parserMap = new HashMap<>();
  for (Column<?> column : table.columns()) {
    AbstractColumnParser<?> parser = column.type().customParser(options);
    parserMap.put(column.name(), parser);
  }
  return parserMap;
}
 
Example 16
Source File: TableSliceGroup.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private boolean containsAnyTextColumns(Table original) {
  for (Column<?> column : original.columns()) {
    if (column.type().equals(ColumnType.TEXT)) {
      return true;
    }
  }
  return false;
}
 
Example 17
Source File: Summarizer.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
private Table combineTables(List<Table> tables) {
  Preconditions.checkArgument(!tables.isEmpty());

  Table result = tables.get(0);
  for (int i = 1; i < tables.size(); i++) {
    Table table = tables.get(i);
    for (Column<?> column : table.columns()) {
      if (tableDoesNotContain(column.name(), result)) {
        result.addColumns(column);
      }
    }
  }
  return result;
}
 
Example 18
Source File: Heatmap.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
public static Figure create(String title, Table table, String categoryCol1, String categoryCol2) {
  Layout layout = Layout.builder(title).build();

  Table counts = table.xTabCounts(categoryCol1, categoryCol2);
  counts = counts.dropRows(counts.rowCount() - 1);
  List<Column<?>> columns = counts.columns();
  columns.remove(counts.columnCount() - 1);
  Column<?> yColumn = columns.remove(0);
  double[][] z = DoubleArrays.to2dArray(counts.numericColumns());

  Object[] x = counts.columnNames().toArray();
  Object[] y = yColumn.asObjectArray();
  HeatmapTrace trace = HeatmapTrace.builder(x, y, z).build();
  return new Figure(layout, trace);
}
 
Example 19
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) {
    }
  }
 
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) {
    }
  }