Sunday, 19 April 2020

TableSaw: Get summary of table

‘table.summarize’ method used to get the summary of a table.

For Example, below statement return a table that contain the summary of Employee Salaries by FirstName.

Table summary = table.summarize("Salary", mean, sum, min, max).by("FirstName");

App.java
package com.sample.app;

import static tech.tablesaw.aggregate.AggregateFunctions.max;
import static tech.tablesaw.aggregate.AggregateFunctions.mean;
import static tech.tablesaw.aggregate.AggregateFunctions.min;
import static tech.tablesaw.aggregate.AggregateFunctions.sum;

import tech.tablesaw.api.DoubleColumn;
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 };
    String[] firstNames = { "Hari", "Ram", "Ram", "Chamu", "Swethank", "Chamu" };
    String[] lastNames = { "Krishna", "Gurram", "Maj", "Maj", "Baji", "Gurram" };
    double[] salary = { 12345, 800000, 545000, 625000, 1200000, 900000 };

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

    Table summary = table.summarize("Salary", mean, sum, min, max).by("FirstName");

    summary.setName("Summary of Employee Salaries by FirstName");

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

  }
}

Output
Employee Id  |  FirstName  |  LastName  |   Salary    |
--------------------------------------------------------
           1  |       Hari  |   Krishna  |    12345.0  |
           2  |        Ram  |    Gurram  |   800000.0  |
           3  |        Ram  |       Maj  |   545000.0  |
           4  |      Chamu  |       Maj  |   625000.0  |
           5  |   Swethank  |      Baji  |  1200000.0  |
           6  |      Chamu  |    Gurram  |   900000.0  |

                          Summary of Employees by Salary                         
 FirstName  |  Mean [Salary]  |  Sum [Salary]  |  Min [Salary]  |  Max [Salary]  |
----------------------------------------------------------------------------------
     Chamu  |       762500.0  |     1525000.0  |      625000.0  |      900000.0  |
      Hari  |        12345.0  |       12345.0  |       12345.0  |       12345.0  |
       Ram  |       672500.0  |     1345000.0  |      545000.0  |      800000.0  |

  Swethank  |      1200000.0  |     1200000.0  |     1200000.0  |     1200000.0  |




Previous                                                    Next                                                    Home

No comments:

Post a Comment