Java 数据清洗
Dennis Lee
Software Engineer
| Books | Authors | Language | First_Published | Sales_in_Millions | Average_Price |
|---|---|---|---|---|---|
| A Tale of Two Cities | Charles Dickens | English | 1859 | 200.0 | 12.99 |
| The Little Prince (Le Petit Prince) | Antoine de Saint-Exupéry | French | 1943 | 200.0 | 15.50 |
| Harry Potter and the Philosopher's Stone | J. K. Rowling | English | 1997 | 120.0 | 19.99 |
| And Then There Were None | Agatha Christie | English | 1939 | 100.0 | 14.95 |
import tech.tablesaw.api.Table;
import tech.tablesaw.selection.Selection;
Table books = Table.read().csv("bestsellers.csv"); // 销量超过 7000 万的条件 Selection highSales = books.intColumn("Sales_in_Millions").isGreaterThan(70);// 英文书籍的条件 Selection english = books.stringColumn("Language").isEqualTo("English");// 1950 年后出版的条件 Selection recentlyPublished = books.intColumn("First_Published").isGreaterThan(1950);// 类似方法:.isLessThan(), .isLessThanOrEqualTo(), .isGreaterThanOrEqualTo()
// 使用单个条件直接筛选 books.where(books.intColumn("Sales_in_Millions").isGreaterThan(70));// 使用预定义的 Selection 筛选 books.where(highSales);// 用 .and() 组合多个条件 Table popular = books.where(highSales) // 销量 > 7000 万 .and(english) // 英文 .and(recentlyPublished); // 1950 年后 .sortDescendingOn("Sales_in_Millions"); // 按 Sales_in_Millions 降序 System.out.println("现代高营收英文书:\n"); System.out.println(modern.select("Books", "First_Published", "Total_Revenue"));
现代高营收英文书:
| Book | Sales_in_Millions | First_Published |
|------------------------------------------|-------------------|-----------------|
| Harry Potter and the Philosopher's Stone | 120 | 1997 |
| The Da Vinci Code | 80 | 2003 |
| Harry Potter and the Chamber of Secrets | 77 | 1998 |
// 导入均值函数
import static tech.tablesaw.aggregate.AggregateFunctions.mean;
Table overallMean = books.summarize("Sales_in_Millions", mean).apply();
System.out.println("\n总体平均销量:\n" + overallMean);
总体平均销量:
| Mean [Sales_in_Millions] |
|---------------------------|
| 73.03225806451614 |
// 按语言分组计算平均销量(如英语、法语等) Table salesByLanguage = books.summarize("Sales_in_Millions", mean) // 计算均值.by("Language"); // 按语言分组 // 显示前 5 个语言组 System.out.println("各语言平均销量:\n"); System.out.println(salesByLanguage.first(5));
各语言平均销量:
| Language | Mean [Sales_in_Millions] |
|------------|--------------------------|
| English | 69.96 |
| French | 200.0 |
| Chinese | 100.0 |
| Portuguese | 65.0 |
| Spanish | 50.0 |
import static tech.tablesaw.aggregate.AggregateFunctions.sum;
// 过滤并按语言汇总销量 Table bestsellersByLanguage = books // 选择 1950 年后的书籍 .where(books.intColumn("First_Published").isGreaterThan(1950))// 计算销量总和 .summarize("Sales_in_Millions", sum)// 按语言分组 .by("Language"); System.out.println("1950 年后畅销书(按语言):\n"); System.out.println(bestsellersByLanguage);
1950 年后畅销书(按语言):
| Language | Sum [Sales_in_Millions] |
|------------|-------------------------|
| English | 1071 |
| Portuguese | 65 |
| Spanish | 50 |
| Italian | 50 |
books.where(sales.isGreaterThan(150)); // 单条件基础筛选
books.where(highSales.and(english)); // 多条件:用 .and() 组合
// 基础聚合
books.summarize("Sales_in_Millions", mean) // 计算均值
.by("Language"); // 按语言分组
// 组合操作
books.where(books.intColumn("First_Published").isGreaterThan(1950)) // 先筛选
.summarize("Sales_in_Millions", mean) // 再汇总
.by("Language"); // 最后分组
Java 数据清洗