Java 数据清洗
Dennis Lee
Software Engineer
| 书名 | 作者 | 语言 | First_Published | Sales_in_Millions | Average_Price |
|---|---|---|---|---|---|
| 双城记 | Charles Dickens | English | 1859 | 200.0 | 12.99 |
| 小王子(Le Petit Prince) | Antoine de Saint-Exupéry | French | 1943 | 200.0 | 15.50 |
| 哈利·波特与魔法石 | J. K. Rowling | English | 1997 | 120.0 | 19.99 |
| 无人生还 | Agatha Christie | English | 1939 | 100.0 | 14.95 |
import tech.tablesaw.api.Table;
import tech.tablesaw.api.StringColumn;
import tech.tablesaw.api.DoubleColumn;
Table books = Table.read().csv("bestsellers.csv"); for (String colName : books.columnNames()) {// 打印列名与其类型 System.out.println(colName + ": " + books.column(colName).type()); }
Books: STRING
Authors: STRING
Language: STRING
First_Published: INTEGER
Sales_in_Millions: INTEGER
Average_Price: DOUBLE
// 将 IntColumn 转为 DoubleColumn,因为 Tablesaw 推断为 INTEGER
DoubleColumn sales = books.intColumn("Sales_in_Millions")
.asDoubleColumn();
System.out.println("Data type of sales column after conversion: " + sales.type());
Data type of sales column after conversion: DOUBLE
// 也可将 DoubleColumn 转为 IntColumn
IntColumn salesAsInt = sales.asIntColumn();
DoubleColumn averagePrice = books.doubleColumn("Average_Price"); // 通过两列相乘计算总收入 DoubleColumn totalRevenue = sales.multiply(averagePrice).setName("Total_Revenue_Millions");books.addColumns(totalRevenue); System.out.println("\n两列相乘:Sales × Price = Revenue"); System.out.println(books.selectColumns("Books", "Sales_in_Millions", "Average_Price", "Total_Revenue_Millions") .first(4).print());
两列相乘:Sales × Price = Revenue
| 书名 | Sales_in_ | Average_ | Total_Revenue_ |
| | Millions | Price | Millions |
|-------------------------------------|------------|-----------|----------------|
| 双城记 | 200 | 12.99 | 2598 |
| 小王子(Le Petit Prince) | 200 | 15.50 | 3100 |
| 哈利·波特与 | 120 | 19.99 | 2398.80 |
| 魔法石 | | | |
| 无人生还 | 100 | 14.95 | 1495 |
\\(.*\\) 匹配括号内容\\( // 匹配左括号(用 \ 转义)
.* // 匹配任意字符(.)零次或多次(*)
\\) // 匹配右括号(用 \ 转义)
// 从标题中移除括号内的翻译
StringColumn titlesNoParentheses = titles
// 对每个标题,去掉括号及其两侧空格
.map(t -> t.replaceAll("\\(.*\\)", "").trim())
// 设置新列名
.setName("去除括号的标题");
System.out.println("Original: " + titles.get(1)); // 获取第一个项
System.out.println("Cleaned: " + titlesNoParentheses.get(1));
Original: The Little Prince (Le Petit Prince)
Cleaned: The Little Prince
// 合并去括号与转小写 StringColumn titlesCombinedCleaning = titles.map(t -> t.replaceAll("\\(.*\\)", "") // 去括号.trim() // 去两侧空格.toLowerCase()) // 转小写 .setName("Clean_Titles_Combined"); // 设列名 System.out.println("Original title: " + titles.get(1)); System.out.println("Title after cleaning: " + titlesCombinedCleaning.get(1));
Original title: The Little Prince (Le Petit Prince)
Title after cleaning: the little prince
books.column(colName).type(); // 获取列类型
books.intColumn("Sales_in_Millions").asDoubleColumn(); // 转换类型
DoubleColumn totalRevenue = sales.multiply(averagePrice) // 列相乘
.setName("Total_Revenue_Millions"); // 新列名
titles.map(String::toLowerCase) // 转小写
.setName("Lowercase_Titles"); // 新列名
titles.map(t -> t.replaceAll("\\(.*\\)", "").trim()) // 去括号
.setName("去除括号的标题"); // 新列名
Java 数据清洗