Sunday 29 March 2020

TableSaw: where: filter the elements that matches given criteria

‘where’ function is used to filter the elements that matches given criteria.
IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30));

Above statement return new filter that contains the elements that are < 30.

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 2, 4, 103, 24, 53, 23, 12, 11, 56, 6 };

  IntColumn intColumn = IntColumn.create("My Numbers", numbers);

  IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30));


  System.out.println(intColumn.print() + "\n");
  
  filteredElements.setName("intColumn < 30");
  System.out.println(filteredElements.print());
 }
}

Output
Column: My Numbers
2
4
103
24
53
23
12
11
56
6


Column: intColumn < 30
2
4
24
23
12
11
6

One advantage of ‘where’ function is, you can combine the filters.

Example
IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30).and(intColumn.isGreaterThan(20)));

App.java
package com.sample.app;

import tech.tablesaw.api.IntColumn;

public class App {

 public static void main(String args[]) {
  int[] numbers = { 2, 4, 103, 24, 53, 23, 12, 11, 56, 6 };

  IntColumn intColumn = IntColumn.create("My Numbers", numbers);

  IntColumn filteredElements = intColumn.where(intColumn.isLessThan(30).and(intColumn.isGreaterThan(20)));

  System.out.println(intColumn.print() + "\n");

  filteredElements.setName("intColumn < 30 && intColumn > 20");
  System.out.println(filteredElements.print());
 }
}

Output

Column: My Numbers
2
4
103
24
53
23
12
11
56
6


Column: intColumn < 30 && intColumn > 20
24
23


Previous                                                    Next                                                    Home

No comments:

Post a Comment