Sunday 5 April 2020

Table: Remove specific columns from a table

TableSaw provides below methods to remove columns from a table.

public Table removeColumns(Column<?>... columns)
public Relation removeColumns(int... columnIndexes)
public Relation removeColumns(String... columnName)

App.java
package com.sample.app;

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, 6, 7, 8, 9 };
  String[] firstNames = { "Hari", "Ram", "Chamu", "Sowmya", "Harini", "Lahari", "Rama", "Lakshman", "Sandya" };
  String[] lastNames = {"Krishna", "Gurram", "Maj", "Dev", "Gurram", "Ram", "Sen", "Grandi", "Neelam"};

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

 
  
  System.out.println(table.print());
  
  table.removeColumns("FirstName");
  System.out.println("\nRemoving the column FirstName\n");
  
  System.out.println(table.print());
  
 }
}

Output
Employee Ids  |  FirstName  |  LastName  |
-------------------------------------------
            1  |       Hari  |   Krishna  |
            2  |        Ram  |    Gurram  |
            3  |      Chamu  |       Maj  |
            4  |     Sowmya  |       Dev  |
            5  |     Harini  |    Gurram  |
            6  |     Lahari  |       Ram  |
            7  |       Rama  |       Sen  |
            8  |   Lakshman  |    Grandi  |
            9  |     Sandya  |    Neelam  |

Removing the column FirstName

 Employee Ids  |  LastName  |
-----------------------------
            1  |   Krishna  |
            2  |    Gurram  |
            3  |       Maj  |
            4  |       Dev  |
            5  |    Gurram  |
            6  |       Ram  |
            7  |       Sen  |
            8  |    Grandi  |
            9  |    Neelam  |




Previous                                                    Next                                                    Home

No comments:

Post a Comment