Saturday, 11 April 2020

TableSaw: Select n random rows from a table

'table.sampleN(noOfRows)' return n number of rows from given table.

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 };
  String[] firstNames = { "Hari", "Ram", "Sowmya", "Chamu", "Hareesh" };
  String[] lastNames = { "Krishna", "Gurram", "Maj", "Dev", "Baji" };

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

  Table randomData = table.sampleN(3);
  System.out.println(randomData.print());
 }
}

Output
Employee Id  |  FirstName  |  LastName  |
------------------------------------------
           1  |       Hari  |   Krishna  |
           2  |        Ram  |    Gurram  |
           4  |      Chamu  |       Dev  |


Previous                                                    Next                                                    Home

No comments:

Post a Comment