0

How can I retrieve the original rows' index after performing a query on the dataset?

I'am using JTablesaw as a Dataset library in Java, and I'm looking for a way to get the original rows' index after performing a query/filter on it.

E.g. considering this dataset:

A,B,C
10,5,3
4,7,2
6,9,1

and the below code

Table table = Table.read().file("data.csv");
Table result = table.where(rows -> rows.intColumn("A").isGreaterThanOrEqualTo(6));

With the result table being:

A,B,C
10,5,3
6,9,1

How can I get in output the orginal rows index? E.g.

[0,2]
1Z10
  • 2,801
  • 7
  • 33
  • 82

2 Answers2

1

Before filtering, you can create an index column using the static method IntColumn.indexColumn() and providing the necessary arguments. For example

table.addColumns(IntColumn.indexColumn("Index", table.rowCount(), 0));

This creates an index column that starts with 0.

L. Blanc
  • 2,150
  • 2
  • 21
  • 31
0

Filtering creates a new Table object and lacks the option to pull the rows' indices from the original table.

One solution is to create a method like below to allow some sort of dynamic iteration over the original table in order to obtain the indices:

    private List<Integer> collectIndices( Table table, String columnName, int condition ) {
        List<Integer> indices = new ArrayList<>();
        for ( int i = 0; i < table.rowCount(); i++ ) {
            if ( table.row( i ).getInt( columnName ) >= condition ) {
                indices.add( i );
            }
        }
        return indices;
    }

collectIndices( table, "A", 6 ); returns [0, 2]

TimonNetherlands
  • 1,033
  • 1
  • 6
  • 6