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

The following examples show how to use tech.tablesaw.api.Table#removeColumns() . 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: 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 2
Source File: FixedWidthReaderTest.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCarsData() throws Exception {

  Table table =
      Table.read()
          .usingOptions(
              FixedWidthReadOptions.builder("../data/fixed_width_cars_test.txt")
                  .header(true)
                  .columnTypes(car_types)
                  .columnSpecs(car_fields_specs)
                  .padding('_')
                  .systemLineEnding()
                  .build());

  assertEquals("[Year, Make, Model, Description, Price]", table.columnNames().toString());

  table = table.sortDescendingOn("Year");
  table.removeColumns("Description");

  assertEquals("[Year, Make, Model, Price]", table.columnNames().toString());
}
 
Example 3
Source File: FixedWidthReaderTest.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithSkipTrailingCharsUntilNewline() throws Exception {

  Table table =
      Table.read()
          .usingOptions(
              FixedWidthReadOptions.builder("../data/fixed_width_wrong_line_length.txt")
                  .header(true)
                  .columnTypes(car_types)
                  .columnSpecs(car_fields_specs)
                  .padding('_')
                  .systemLineEnding()
                  .skipTrailingCharsUntilNewline(true)
                  .build());

  assertEquals("[Year, Make, Model, Description, Price]", table.columnNames().toString());

  table = table.sortDescendingOn("Year");
  table.removeColumns("Price");

  assertEquals("[Year, Make, Model, Description]", table.columnNames().toString());
}
 
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: FixedWidthReaderTest.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithCarsData() throws Exception {

  Table table =
      Table.read()
          .usingOptions(
              FixedWidthReadOptions.builder("../data/fixed_width_cars_test.txt")
                  .header(true)
                  .columnTypes(car_types)
                  .columnSpecs(car_fields_specs)
                  .padding('_')
                  .systemLineEnding()
                  .build());

  assertEquals("[Year, Make, Model, Description, Price]", table.columnNames().toString());

  table = table.sortDescendingOn("Year");
  table.removeColumns("Description");

  assertEquals("[Year, Make, Model, Price]", table.columnNames().toString());
}
 
Example 6
Source File: FixedWidthReaderTest.java    From tablesaw with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithSkipTrailingCharsUntilNewline() throws Exception {

  Table table =
      Table.read()
          .usingOptions(
              FixedWidthReadOptions.builder("../data/fixed_width_wrong_line_length.txt")
                  .header(true)
                  .columnTypes(car_types)
                  .columnSpecs(car_fields_specs)
                  .padding('_')
                  .systemLineEnding()
                  .skipTrailingCharsUntilNewline(true)
                  .build());

  assertEquals("[Year, Make, Model, Description, Price]", table.columnNames().toString());

  table = table.sortDescendingOn("Year");
  table.removeColumns("Price");

  assertEquals("[Year, Make, Model, Description]", table.columnNames().toString());
}
 
Example 7
Source File: ColumnTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
void testColumnDelete() {
  final Table table =
      Table.create(
          BooleanColumn.create("a"), BooleanColumn.create("b"), BooleanColumn.create("c"));
  assertEquals(3, table.columnCount());
  table.removeColumns(table.columnNames().indexOf("b"));
  assertEquals(2, table.columnCount());
}
 
Example 8
Source File: CsvReaderTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBusData() throws IOException {
  // Read the CSV file
  Table table =
      Table.read()
          .csv(CsvReadOptions.builder("../data/bus_stop_test.csv").columnTypes(bus_types));

  // Look at the column names
  assertEquals(
      "[stop_id, stop_name, stop_desc, stop_lat, stop_lon]", table.columnNames().toString());

  table = table.sortDescendingOn("stop_id");
  table.removeColumns("stop_desc");
}
 
Example 9
Source File: ColumnTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
void testColumnDelete() {
  final Table table =
      Table.create(
          BooleanColumn.create("a"), BooleanColumn.create("b"), BooleanColumn.create("c"));
  assertEquals(3, table.columnCount());
  table.removeColumns(table.columnNames().indexOf("b"));
  assertEquals(2, table.columnCount());
}
 
Example 10
Source File: CsvReaderTest.java    From tablesaw with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithBusData() throws IOException {
  // Read the CSV file
  Table table =
      Table.read()
          .csv(CsvReadOptions.builder("../data/bus_stop_test.csv").columnTypes(bus_types));

  // Look at the column names
  assertEquals(
      "[stop_id, stop_name, stop_desc, stop_lat, stop_lon]", table.columnNames().toString());

  table = table.sortDescendingOn("stop_id");
  table.removeColumns("stop_desc");
}
 
Example 11
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 12
Source File: BusStopExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    out("");
    out("Some Examples: ");

    // Read the CSV file
    ColumnType[] types = {
      ColumnType.DOUBLE, ColumnType.STRING, ColumnType.STRING, ColumnType.DOUBLE, ColumnType.DOUBLE
    };
    Table table =
        Table.read().csv(CsvReadOptions.builder("../data/bus_stop_test.csv").columnTypes(types));

    // Look at the column names
    out(table.columnNames());

    // Peak at the data
    out(table.first(5));

    // Remove the description column
    table.removeColumns("stop_desc");

    // Check the column names to see that it's gone
    out(table.columnNames());

    // Take a look at some data
    out("In 'examples. Printing first(5)");
    out(table.first(5));

    // Lets take a look at the latitude and longitude columns
    // out(table.realColumn("stop_lat").rowSummary().out());
    out(table.numberColumn("stop_lat").summary());

    // Now lets fill a column based on data in the existing columns

    // Apply the map function and fill the resulting column to the original table

    // Lets filtering out some of the rows. We're only interested in records with IDs between
    // 524-624

    Table filtered = table.where(table.numberColumn("stop_id").isBetweenInclusive(524, 624));
    out(filtered.first(5));

    // Write out the new CSV file
    filtered.write().csv("../data/filtered_bus_stops.csv");
  }
 
Example 13
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 14
Source File: BusStopExample.java    From tablesaw with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {

    out("");
    out("Some Examples: ");

    // Read the CSV file
    ColumnType[] types = {
      ColumnType.DOUBLE, ColumnType.STRING, ColumnType.STRING, ColumnType.DOUBLE, ColumnType.DOUBLE
    };
    Table table =
        Table.read().csv(CsvReadOptions.builder("../data/bus_stop_test.csv").columnTypes(types));

    // Look at the column names
    out(table.columnNames());

    // Peak at the data
    out(table.first(5));

    // Remove the description column
    table.removeColumns("stop_desc");

    // Check the column names to see that it's gone
    out(table.columnNames());

    // Take a look at some data
    out("In 'examples. Printing first(5)");
    out(table.first(5));

    // Lets take a look at the latitude and longitude columns
    // out(table.realColumn("stop_lat").rowSummary().out());
    out(table.numberColumn("stop_lat").summary());

    // Now lets fill a column based on data in the existing columns

    // Apply the map function and fill the resulting column to the original table

    // Lets filtering out some of the rows. We're only interested in records with IDs between
    // 524-624

    Table filtered = table.where(table.numberColumn("stop_id").isBetweenInclusive(524, 624));
    out(filtered.first(5));

    // Write out the new CSV file
    filtered.write().csv("../data/filtered_bus_stops.csv");
  }