66import tech .tablesaw .api .StringColumn ;
77import tech .tablesaw .api .Table ;
88import tech .tablesaw .columns .Column ;
9+ import tech .tablesaw .io .csv .CsvReadOptions ;
910
1011import com .google .common .base .Stopwatch ;
1112
1920 */
2021public class TestTablesaw {
2122 public static void main (String [] args ) throws Exception {
22- Table data = Table .read ().csv ("urb_cpop1_1_Data.csv" );
23+ // This automatically makes the ":" values missing
24+ Table data = Table .read ().csv (
25+ CsvReadOptions .builder ("urb_cpop1_1_Data.csv" ).missingValueIndicator (":" ).build ());
2326 System .out .println (data .print (5 ));
2427
2528 Stopwatch watch = Stopwatch .createStarted ();
26- // Convert value column to numeric column (":" should become missing)
27- Table filtered = data .emptyCopy ();
28- filtered .replaceColumn ("Value" , DoubleColumn .create ("Value" ));
29- data .forEach (row -> {
30- if (!":" .equals (row .getString ("Value" ))) {
31- for (int i = 0 ; i < row .columnCount (); i ++) {
32- String colName = data .column (i ).name ();
33- Object obj = row .getObject (i );
34- if ("Value" .equals (colName )) {
35- obj = Double .parseDouble ((String )obj );
36- }
37- filtered .column (colName ).appendObj (obj );
38- }
39- }
40- });
29+ Table filtered = data .where (data .column ("Value" ).isNotMissing ());
4130
4231 Table cities = filtered .summarize ("Value" , mean ).by ("CITIES" , "INDIC_UR" , "TIME" );
4332 System .out .println (cities .print (10 ));
4433
4534 // Need to transpose/pivot now too
35+ // Next version of tablesaw will make this a one-liner!
4636 Table finalTable = Table .create ("final" );
4737 finalTable .addColumns (StringColumn .create ("key" ));
4838 cities .forEach (row -> {
49- int year = ( int ) row .getDouble ("TIME" );
39+ int year = row .getShort ("TIME" );
5040 String yearStr = Integer .toString (year );
5141 String key = row .getString ("CITIES" ) + ":" + row .getString ("INDIC_UR" );
5242 double value = row .getDouble ("Mean [Value]" );
@@ -74,16 +64,11 @@ public static void main(String[] args) throws Exception {
7464 System .out .println (filterTotalKeys (existing2017 ).sortDescendingOn ("2017" ).print (20 ));
7565
7666 // Add growth column
77- DoubleColumn growthColumn = DoubleColumn .create ("growth" );
78- finalTable .forEach (row -> {
79- Double val2016 = row .getDouble ("2016" ), val2010 = row .getDouble ("2010" );
80- if (val2010 != null && val2016 != null ) {
81- growthColumn .append ((val2016 / val2010 - 1 ) * 100 );
82- } else {
83- growthColumn .appendMissing ();
84- }
85- });
67+ DoubleColumn growthColumn = finalTable .doubleColumn ("2016" ).divide (
68+ finalTable .doubleColumn ("2010" )).subtract (1 ).multiply (100 );
69+ growthColumn .setName ("growth" );
8670 finalTable .addColumns (growthColumn );
71+
8772 Table highestGrowthTable = filterTotalKeys (
8873 finalTable .dropWhere (finalTable .column ("growth" ).isMissing ())).sortDescendingOn (
8974 "growth" );
@@ -94,15 +79,6 @@ public static void main(String[] args) throws Exception {
9479 }
9580
9681 private static Table filterTotalKeys (Table existing2017 ) {
97- // Here I'd want to do something more functional like
98- // return existing2017.filter(row -> row.getString("key").endsWith(...))
99- // but there doesn't seem to be an easy way to do that.
100- Table onlyTotalKeys = existing2017 .emptyCopy ();
101- existing2017 .forEach (row -> {
102- if (row .getString ("key" ).endsWith ("January, total" )) {
103- onlyTotalKeys .addRow (row );
104- }
105- });
106- return onlyTotalKeys ;
82+ return existing2017 .where (existing2017 .stringColumn ("key" ).endsWith ("January, total" ));
10783 }
10884}
0 commit comments