Skip to content

Commit 810f99f

Browse files
committed
update tablesaw to 0.37.3, use pivot method
1 parent 1dc774c commit 810f99f

File tree

2 files changed

+6
-28
lines changed

2 files changed

+6
-28
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ repositories {
99
}
1010

1111
dependencies {
12-
compile 'tech.tablesaw:tablesaw-core:0.30.1'
12+
compile 'tech.tablesaw:tablesaw-core:0.37.3'
1313

1414
compile 'joinery:joinery-dataframe:1.9'
1515
// For the CSV import joinery needs this dependency too:

src/main/java/test_dataframes/TestTablesaw.java

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import tech.tablesaw.api.DoubleColumn;
66
import tech.tablesaw.api.StringColumn;
77
import tech.tablesaw.api.Table;
8-
import tech.tablesaw.columns.Column;
98
import tech.tablesaw.io.csv.CsvReadOptions;
109

1110
import 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

Comments
 (0)