Showing posts with label missing values. Show all posts
Showing posts with label missing values. Show all posts

Saturday, 11 April 2020

TableSaw: dropRowsWithMissingValues: Drop rows with missing values

‘dropRowsWithMissingValues’ method is used to drop the rows with missing values.

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

            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));

            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.dropRowsWithMissingValues();
            System.out.println("After dropping duplicate rows\n");
            
            System.out.println(table.print());

      }
}

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

After dropping duplicate rows

 Employee Ids  |  FirstName  |  LastName  |  Date Of Birth  |
-------------------------------------------------------------
            1  |       Hari  |   Krishna  |     1988-05-01  |
            2  |        Ram  |    Gurram  |     1976-08-01  |
            4  |      Chamu  |       Dev  |     1968-05-21  |




Previous                                                    Next                                                    Home

Monday, 6 April 2020

TableSaw: Remove columns with missing values

‘table.removeColumnsWithMissingValues’ method remove the columns with missing values.

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", null, null, null, "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.removeColumnsWithMissingValues();

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

    }
}

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


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

As you see in the above example, Column FirstName has missing values for the rows 2, 3, and 4. When I call the method ‘removeColumnsWithMissingValues’ it removes the column FirstName.




Previous                                                    Next                                                    Home