55import tech .tablesaw .api .DoubleColumn ;
66import tech .tablesaw .api .StringColumn ;
77import tech .tablesaw .api .Table ;
8- import tech .tablesaw .columns .Column ;
98import tech .tablesaw .io .csv .CsvReadOptions ;
109
1110import com .google .common .base .Stopwatch ;
@@ -32,32 +31,11 @@ public static void main(String[] args) throws Exception {
3231 System .out .println (cities .print (10 ));
3332
3433 // Need to transpose/pivot now too
35- // Next version of tablesaw will make this a one-liner!
36- Table finalTable = Table .create ("final" );
37- finalTable .addColumns (StringColumn .create ("key" ));
38- cities .forEach (row -> {
39- int year = row .getShort ("TIME" );
40- String yearStr = Integer .toString (year );
41- String key = row .getString ("CITIES" ) + ":" + row .getString ("INDIC_UR" );
42- double value = row .getDouble ("Mean [Value]" );
43- if (!finalTable .columnNames ().contains (yearStr )) {
44- DoubleColumn col = DoubleColumn .create (yearStr );
45- // Need to prefill null for rows that exist already.
46- for (int i = 0 ; i < finalTable .rowCount (); i ++) {
47- col .appendMissing ();
48- }
49- finalTable .addColumns (col );
50- }
51- int firstIndex = ((StringColumn )finalTable .column ("key" )).firstIndexOf (key );
52- if (firstIndex == -1 ) {
53- firstIndex = finalTable .rowCount ();
54- for (Column <?> col : finalTable .columns ()) {
55- col .appendMissing ();
56- }
57- ((Column <String >)finalTable .column ("key" )).set (firstIndex , key );
58- }
59- ((Column <Double >)finalTable .column (yearStr )).set (firstIndex , value );
60- });
34+ StringColumn key = filtered .stringColumn ("CITIES" )
35+ .join (":" , filtered .stringColumn ("INDIC_UR" )).setName ("key" );
36+ filtered .addColumns (key );
37+ Table finalTable = filtered .pivot ("key" , "TIME" , "Value" , mean );
38+
6139 // sortDescendingOn puts N/A values first unfortunately, so let's remove them
6240 // before determining and printing.
6341 Table existing2017 = finalTable .dropWhere (finalTable .column ("2017" ).isMissing ());
0 commit comments