轉自:https://blog.csdn.net/qq_44525150/article/details/111768704
參考:https://juejin.cn/post/6844904073670590478
配置
Tablesaw需要Java8或更新的版本。
使用Maven導入包。
<dependency> <groupId>tech.tablesaw</groupId> <artifactId>tablesaw-core</artifactId> <version>LATEST</version> </dependency>
1.表和列
Tablesaw整個都是關於表格的,而表格又是由列組成的,所以我們的學習先從列開始。
列
一個Column是一個被命名的,一維數據集,它可能是一個Table的一部分,也可能不是。一個Column中的數據必須是同一種類型。
創建一個數據列
使用DoubleColumn.create(String name,double ... arr)方法來創建一個元素類型為double的column;
根據包含元素類型的不同,還有IntColumn,StringColumn等。
double[] numbers = {1,2,3,4}; DoubleColumn nc = DoubleColumn.create("nc",numbers); // DoubleColumn nc = DoubleColumn.create("nc",1,2,3,4); 這樣也是可以的 System.out.println(nc.print());
2.獲取數列中的某個數據
使用 nc.get(int index)方法來獲取column某個元素,index從0開始。
// 創建數據列
double[] numbers = {1,2,3,4}; DoubleColumn nc = DoubleColumn.create("nc",numbers); // index為2,所以獲取到nc中的第三個元素 Double three = nc.get(2); System.out.println(three);
3.columnar operations(柱狀運算)
Tablesaw中有很多針對一列數據的運算,如下面的multiply(Number value)是原來列中的每個元素都乘以value。
// 創建數據列 double[] numbers = {1,2,3,4}; DoubleColumn nc1 = DoubleColumn.create("nc",numbers); System.out.println(nc1.print()); // 使用multiply(Number value)是原來列中的每個元素都乘以value // 這個方法並不會修改nc,而是返回一個新的Column對象nc1,nc1的列名為[nc列名*value],我們可以使用 setName(String name),設置列名; DoubleColumn nc2 = nc1.multiply(8); // nc1.setName("nc2"); System.out.println(nc2.print());
4.Selections(選擇項)
Selections被用來篩選表和列。我們對一個Column對象使用形如isLessThan(aNumber)方法來附加篩選條件,就可以生成一個Selection對象,一個Selection對象包含一個有序的int類型的集合,該集合包含了Column對象中滿足設定條件的元素的索引。
// 創建數據列 double[] numbers = {1,2,3,4}; DoubleColumn nc1 = DoubleColumn.create("nc",numbers); // 使用isLessThan(double value)方法 獲取篩選出nc中小於3的元素的Selection對象 Selection lessThanThreeSelection = nc1.isLessThan(3); //A selection maintains an ordered set of ints that can be used to eval rows from a table or column System.out.println(Arrays.toString(lessThanThreeSelection.toArray()));
5.使用where(Selection selection)方法,獲取經過指定selection篩選后的Column或Table
DoubleColumn nc2 = nc1.where(lessThanThreeSelection);
System.out.println(nc2.print());
6.組合過濾器
DoubleColumn nc3 = nc1.where(nc1.isGreaterThan(1).and(nc1.isLessThan(3)));
System.out.println(nc3.print());
7.通過index選擇
DoubleColumn nc4 = nc1.where(Selection.with(1, 3)); System.out.println(nc4.print()); DoubleColumn nc5 = nc1.where(Selection.withRange(1, 3)); System.out.println(nc5.print());
9.Map functions
Map functions是一些定義在columns上的方法,它們返回一個新的Columns,
之前的 Column multiply(Number value)方法就是一個有一個標量參數的map function
我們也可以使用multiply(NumberColumn numberColumn)的方法,來是兩個column中的值對應相乘
DoubleColumn nc1 = DoubleColumn.create("nc",new double[]{1,2,3,4,5}); DoubleColumn nc2 = DoubleColumn.create("nc1",new double[]{1,2,3,4,5}); DoubleColumn nc3 = nc1.multiply(nc2); System.out.println(nc3.print());
Tablesaw針對各種Column類型內置了很多的map functions,下面是一些例子
StringColumn s = StringColumn.create("sc", new String[] {"foo", "bar", "baz", "foobarbaz"}); StringColumn s2 = s.copy(); // 以下方法全部都是針對StringColumn中所有字符串進行的操作 s2 = s2.replaceFirst("foo", "bar"); // 字符串替換 s2 = s2.upperCase(); // 轉化成大寫 s2 = s2.padEnd(5, 'x'); // 字符串的長度小於minlength(第一個參數)時,在字符串的尾部填充某字符,使其長度達到minlength s2 = s2.substring(1, 5); // 獲取[1,5)子串 System.out.println(s2.print());
10.Reduce (aggregate) functions: Summarizing a column(匯總功能)
有時我們想計算一些值從某種意義上總結一列中的數據,匯總函數就是用來做這個的。
DoubleColumn nc1 = DoubleColumn.create("nc",new double[]{1,2,3,4,5}); double max = nc1.max(); // 列中的最大值 System.out.println(max); double min = nc1.min(); // 列中的最小值 System.out.println(min); double sum = nc1.sum(); // 最大值 System.out.println(sum); double stdDev = nc1.standardDeviation(); // 標准差 System.out.println(stdDev);
11.表
一個Table是一個被命名的columns的容器,雖然缺失值是允許的,但是table中所有的column的元素數量應該一樣。
一個Table可以包含任何類型的columns組合。
創建Table
String[] animals = {"bear", "cat", "giraffe"}; double[] cuteness = {90.1, 84.3, 99.7}; // 使用create(String name)方法創建Table對象,使用addColumns(Column<?>...cols)方法向表中添加列 Table cuteAnimals = Table.create("Cute Animals").addColumns( StringColumn.create("Animal types", animals), DoubleColumn.create("rating", cuteness)); System.out.println(cuteAnimals.print());
11.導入數據
很多時候我們會直接從csv文件中導入數據,使用下面的語句可以很方便的從csv文件中讀取一個Table對象,Tablesaw可以很好的猜測出每一列的類型,當它猜錯類型或者是我們想要提高性能的時候可以指明類型。
Table bushTable = Table.read().csv("data/bush.csv");
System.out.println(bushTable.print());
12.瀏覽Tables
Tablesaw提供了一些方法,讓幫助我們了解新數據集的結構,形狀等信息。
Table的結構(列名,列類型)
Table bushTable = Table.read().csv("data/bush.csv");
System.out.println(bushTable.structure());
//Table的形狀(rows*cols) System.out.println(bushTable.shape());
System.out.println(bushTable.first(3));
System.out.println(bushTable.last(3));
//Table的toString()方法也表示為上面那種表格的形式 Table bushTable = Table.read().csv("data/bush.csv"); // 默認只顯示20rows,其他數據會省略; System.out.println(bushTable.toString()); // 我們可以使用printAll()來打印所有的數據 System.out.println(bushTable.printAll()); // 也可以使用print(int rowLimit)來指定打印的行數 System.out.println(bushTable.print(100));
13.處理表中的column
//構造表 Table table = Table.create("table").addColumns(IntColumn.create("col1", 1, 2, 3, 4, 5), IntColumn.create("col2", 2, 4, 6, 8, 0), IntColumn.create("col3", 3, 6, 9, 12, 13)); System.out.println(table); // 移除列 // 移除 名字為"col1"的列 table.removeColumns("col1"); System.out.println(table); // 只保留 名字為"col2"的列 table.retainColumns("col2"); System.out.println(table); // 添加列 table.addColumns(IntColumn.create("col4", 4, 8, 12, 16, 20)); System.out.println(table);
14.從table中獲取特定類型的column
Table studentTable = Table.create("student").addColumns(IntColumn.create("id", 20200001, 20200002, 20200003, 20200004, 20200005), StringColumn.create("name", "Jim", "Bob", "John", "lily", "lucy")); // column(String colName)方法,通過列名獲取指定列,不區分大小寫 Column<?> idCol1 = studentTable.column("id"); System.out.println(idCol1.print()); // column(int colIndex)方法,通過索引獲取指定列,索引從0開始 Column<?> idCol2 = studentTable.column(0); System.out.println(idCol2.print()); // System.out.println(idCol1 == idCol2); // true // 上面的方法返回的都是Column<?>類型,必要時需要轉換成特定的類型 // 1.強制類型轉換 IntColumn idCol3 = (IntColumn) studentTable.column("id"); // 2.使用numberColumn(),stringColumn()等方法 NumericColumn<?> idColumn = studentTable.numberColumn("id");
15.處理行
//刪除行 Table studentTable = Table.create("student").addColumns(IntColumn.create("id", 20200001, 20200001, 20200002, 20200003, 20200004, 20200005), StringColumn.create("name", "Jim", "Jim", "Bob", "John", "lily", "lucy")); System.out.println(studentTable.print()); // drop...()方法不在原表上修改,而是先創建一個和原表結構一樣的空表,然后把不需要刪除的行放到新的而表中,然后返回新的表。 Table table1 = studentTable.dropDuplicateRows(); System.out.println(table1.print()); Table table2 = studentTable.dropWhere(studentTable.numberColumn(0).isLessThan(20200003)); System.out.println(table2); //增加行 // 修改原表 studentTable.addRow(0, studentTable);
16.隨機抽樣
Table table3 = studentTable.sampleN(3); System.out.println(table3.print()); //我們也可以對表中的行執行任意的操作 // 遍歷表中的行,並執行操作 for (Row r : studentTable) { System.out.println("學號:" + r.getString(1) + ":" + r.getString(1)); } // 另外一種實現形式 studentTable.stream().forEach(row -> { System.out.println("學號:" + row.getString(1) + ":" + row.getString(1)); });
17.table排序
Table studentTable = Table.create("student").addColumns(IntColumn.create("id", 20200001,20200002, 20200003, 20200004, 20200005), StringColumn.create("name", "Jim", "Bob", "John", "lily", "lucy")); // 升序 studentTable = studentTable.sortOn("id", "name");// or sortAscendingOn("id","name"); System.out.println(studentTable.print()); // 降序 studentTable = studentTable.sortDescendingOn("id"); System.out.println(studentTable);
18.篩選
查詢filter(過濾器)可以使用邏輯運算符or,and,not來組合,它們都在QuerySupport中實現,所以在使用前要先導入:
import static tech.tablesaw.api.QuerySupport.and; import static tech.tablesaw.api.QuerySupport.or; import static tech.tablesaw.api.QuerySupport.not; Table studentTable = Table.create("student").addColumns(IntColumn.create("id", 20200001, 20200002, 20200003, 20200004, 20200005), StringColumn.create("name", "Jim", "Bob", "John", "lily", "lucy"), IntColumn.create("age", 18, 18, 17, 16, 16)); Table result = studentTable.where(and(t -> t.numberColumn("id").isGreaterThan(20200001), t -> t.numberColumn("age").isEqualTo(18))); System.out.println(result);
19.join
//field tech.tablesaw.api.Table left = sourceConn.query(sql21, rs -> { return tech.tablesaw.api.Table.read().db(rs); }); log.info(left.printAll()); tech.tablesaw.api.Table right = sourceConn.query(sql2, rs -> { return tech.tablesaw.api.Table.read().db(rs); }); log.info(right.printAll()); // left.column("field").setName("col_name"); tech.tablesaw.api.Table result = left.joinOn("field").leftOuter(right,"col_name");