tablesaw是一个Java的数据分析及可视化框架,可以对表格数据进行处理,是面向列的。首先介绍tablesaw对于表格的创建与对文件的导入导出:
1.使用首先添加依赖:
<dependency>
<groupId>tech.tablesaw</groupId>
<artifactId>tablesaw-core</artifactId>
<version>0.38.3</version>
</dependency>
2.自定义表格
tablesaw种有一个自定义的table类
2.1 创建表格:
Table t = Table.create("name", column1, column2,...)
其中column为tablesaw自定义的列类,创建方法如:
IntColumn column = IntColumn.create("a", s1);
2.2 增加列:
t.addColumns(column2);
2.3 增加行:
Table t2 = Table.create("222").addColumns(s21,s22);
Row row = new Row(t2,2);
t1.addRow(row);
或:
t2.addRow(rowIndex, table);
或:
Selection s = Selection.withRange(0,2);
TableSlice ts = new TableSlice(t1,s);
Row row = new Row(ts);
注意:
(1)因为是tablesaw是面向列的,所以在增加行的时候,需要通过创建一个新的table,将这个新的table中的某个行添加到需要的table中。
(2)在创建行的时候,提供了一个只传table的函数,但是这个函数默认的行数是-1,用这个就会报越界。
(3)可以通过修改column/row来修改某一个数据,table建立好后,不能修改超过范围的数据(没有默认值/null)
(4)只能添加行数相同的列,或者通过新的table增加行,增加的行中数据个数大于列数时,会自动新增列,如果小于,没有数目的位置置为?(但是没有修改?的方法)
3.导入&导出数据
通过单独的类用于导入数据:DataFrameReader,由Table.read()获取
支持导入、导出的格式:
读取数据:
读取数据的方法:
使用举例:
Table t = Table.read().file("myFile.csv");
Table t = Table.read().csv("myFile.csv");
注意:
(1)下载的文件如果是压缩文件,需要先解压成数据流再进行读取,不能直接用URL
导出数据
table.write().csv("filename.csv");
注意:
(1)文件需要是标准的表格,否则会报越界
参考:
https://jtablesaw.github.io/tablesaw/ tablesaw官方文档