Làm sạch dữ liệu bằng 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"); // Điều kiện cho sách có doanh số > 70M Selection highSales = books.intColumn("Sales_in_Millions").isGreaterThan(70);// Điều kiện cho sách viết bằng tiếng Anh Selection english = books.stringColumn("Language").isEqualTo("English");// Điều kiện cho sách xuất bản sau năm 1950 Selection recentlyPublished = books.intColumn("First_Published").isGreaterThan(1950);// Tương tự: .isLessThan(), .isLessThanOrEqualTo(), .isGreaterThanOrEqualTo()
// Lọc với một điều kiện trực tiếp books.where(books.intColumn("Sales_in_Millions").isGreaterThan(70));// Lọc bằng Selection đã định nghĩa trước books.where(highSales);// Kết hợp nhiều điều kiện với .and() Table popular = books.where(highSales) // Sales > 70M .and(english) // Ngôn ngữ English .and(recentlyPublished); // Sau 1950 .sortDescendingOn("Sales_in_Millions"); // Sắp xếp theo Sales_in_Millions System.out.println("Sách tiếng Anh doanh thu cao hiện đại:\n"); System.out.println(modern.select("Books", "First_Published", "Total_Revenue"));
Sách tiếng Anh doanh thu cao hiện đại:
| 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 |
// Nhập hàm tính trung bình
import static tech.tablesaw.aggregate.AggregateFunctions.mean;
Table overallMean = books.summarize("Sales_in_Millions", mean).apply();
System.out.println("\nDoanh số trung bình tổng thể: \n" + overallMean);
Doanh số trung bình tổng thể:
| Mean [Sales_in_Millions] |
|---------------------------|
| 73.03225806451614 |
// Tính doanh số trung bình cho từng nhóm ngôn ngữ (ví dụ: English, French,...) Table salesByLanguage = books.summarize("Sales_in_Millions", mean) // Tính trung bình.by("Language"); // Nhóm theo ngôn ngữ // Hiển thị 5 nhóm đầu tiên System.out.println("Doanh số trung bình theo ngôn ngữ:\n"); System.out.println(salesByLanguage.first(5));
Doanh số trung bình theo ngôn ngữ:
| 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;
// Lọc và tổng hợp doanh số theo ngôn ngữ Table bestsellersByLanguage = books // Chọn sách sau năm 1950 .where(books.intColumn("First_Published").isGreaterThan(1950))// Tính tổng doanh số .summarize("Sales_in_Millions", sum)// Nhóm kết quả theo ngôn ngữ .by("Language"); System.out.println("Sách bán chạy hiện đại (sau 1950) theo ngôn ngữ:\n"); System.out.println(bestsellersByLanguage);
Sách bán chạy hiện đại (sau 1950) theo ngôn ngữ:
| Language | Sum [Sales_in_Millions] |
|------------|-------------------------|
| English | 1071 |
| Portuguese | 65 |
| Spanish | 50 |
| Italian | 50 |
books.where(sales.isGreaterThan(150)); // Lọc cơ bản với một điều kiện
books.where(highSales.and(english)); // Nhiều điều kiện - kết hợp bằng .and()
// Tổng hợp cơ bản
books.summarize("Sales_in_Millions", mean) // Tính trung bình
.by("Language"); // Nhóm theo ngôn ngữ
// Kết hợp thao tác
books.where(books.intColumn("First_Published").isGreaterThan(1950)) // Lọc
.summarize("Sales_in_Millions", mean) // Rồi tổng hợp
.by("Language"); // Cuối cùng nhóm
Làm sạch dữ liệu bằng Java