Saturday 11 April 2020

TableSaw: Drop rows that satisfy the condition

‘table.dropWhere’ method used to drop the rows that satisfy the condition.

For example, below statement drops the rows, where firstName starts with ‘Ha’.
table = table.dropWhere(table.stringColumn(1).startsWith("Ha"));

App.java
package com.sample.app;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

import tech.tablesaw.api.DateColumn;
import tech.tablesaw.api.IntColumn;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.Table;

public class App {

      public static void main(String args[]) {
            int[] empIds = { 1, 2, 3, 4, 5};
            String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Hareesh" };
            String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Baji"};

            List<LocalDate> dateOfBirths = Arrays.asList(LocalDate.of(1988, 5, 1), LocalDate.of(1976, 8, 1),
                        LocalDate.of(1948, 5, 11), LocalDate.of(1968, 5, 21), LocalDate.of(1948, 5, 11));

            Table table = Table.create().addColumns(IntColumn.create("Employee Ids", empIds))
                        .addColumns(StringColumn.create("FirstName", firstNames))
                        .addColumns(StringColumn.create("LastName", lastNames))
                        .addColumns(DateColumn.create("Date Of Birth", dateOfBirths));

            System.out.println(table.print() + "\n");
            
            table = table.dropWhere(table.stringColumn(1).startsWith("Ha"));
            
            System.out.println("Dropping the rows, where firstName starts with 'Ha'\n");
            
            System.out.println(table.print() + "\n");

      }
}

Output

Employee Ids  |  FirstName  |  LastName  |  Date Of Birth  |
-------------------------------------------------------------
            1  |       Hari  |   Krishna  |     1988-05-01  |
            2  |        Ram  |    Gurram  |     1976-08-01  |
            3  |     Sowmya  |       Maj  |     1948-05-11  |
            4  |      Chamu  |       Dev  |     1968-05-21  |
            5  |    Hareesh  |      Baji  |     1948-05-11  |

Dropping the rows, where firstName starts with 'Ha'

 Employee Ids  |  FirstName  |  LastName  |  Date Of Birth  |
-------------------------------------------------------------
            2  |        Ram  |    Gurram  |     1976-08-01  |
            3  |     Sowmya  |       Maj  |     1948-05-11  |
            4  |      Chamu  |       Dev  |     1968-05-21  |





Previous                                                    Next                                                    Home

No comments:

Post a Comment