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.api.StringColumn;
import tech.tablesaw.api.DoubleColumn;
Table books = Table.read().csv("bestsellers.csv"); for (String colName : books.columnNames()) {// In tên cột và kiểu dữ liệu System.out.println(colName + ": " + books.column(colName).type()); }
Books: STRING
Authors: STRING
Language: STRING
First_Published: INTEGER
Sales_in_Millions: INTEGER
Average_Price: DOUBLE
// Chuyển IntColumn sang DoubleColumn vì Tablesaw nhận dạng là INTEGER
DoubleColumn sales = books.intColumn("Sales_in_Millions")
.asDoubleColumn();
System.out.println("Kiểu dữ liệu của cột sales sau khi chuyển: " + sales.type());
Data type of sales column after conversion: DOUBLE
// Có thể chuyển DoubleColumn sang IntColumn
IntColumn salesAsInt = sales.asIntColumn();
DoubleColumn averagePrice = books.doubleColumn("Average_Price"); // Tính tổng doanh thu bằng cách nhân hai cột DoubleColumn totalRevenue = sales.multiply(averagePrice).setName("Total_Revenue_Millions");books.addColumns(totalRevenue); System.out.println("\nNhân hai cột: Sales × Price = Revenue"); System.out.println(books.selectColumns("Books", "Sales_in_Millions", "Average_Price", "Total_Revenue_Millions") .first(4).print());
Nhân hai cột: Sales * Price = Revenue
| Sách | Sales_in_ | Average_ | Total_Revenue_ |
| | Millions | Price | Millions |
|-------------------------------------|------------|-----------|----------------|
| A Tale of Two Cities | 200 | 12.99 | 2598 |
| The Little Prince (Le Petit Prince) | 200 | 15.50 | 3100 |
| Harry Potter and | 120 | 19.99 | 2398.80 |
| the Philosopher's Stone | | | |
| And Then There Were None | 100 | 14.95 | 1495 |
\\(.*\\) khớp dấu ngoặc tròn\\( // Khớp ngoặc mở (escape bằng \)
.* // Khớp mọi ký tự (.) lặp 0+ lần (*)
\\) // Khớp ngoặc đóng (escape bằng \)
// Xóa phần dịch trong ngoặc khỏi tiêu đề
StringColumn titlesNoParentheses = titles
// Với mỗi tiêu đề, xóa ngoặc và khoảng trắng xung quanh
.map(t -> t.replaceAll("\\(.*\\)", "").trim())
// Đặt tên cột mới
.setName("Titles without parentheses");
System.out.println("Original: " + titles.get(1)); // Lấy phần tử đầu tiên
System.out.println("Cleaned: " + titlesNoParentheses.get(1));
Original: The Little Prince (Le Petit Prince)
Cleaned: The Little Prince
// Kết hợp xóa ngoặc và chuyển thành chữ thường StringColumn titlesCombinedCleaning = titles.map(t -> t.replaceAll("\\(.*\\)", "") // Xóa ngoặc.trim() // Xóa khoảng trắng xung quanh.toLowerCase()) // Chuyển thành chữ thường .setName("Clean_Titles_Combined"); // Đặt tên cột 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(); // Lấy kiểu cột
books.intColumn("Sales_in_Millions").asDoubleColumn(); // Chuyển kiểu
DoubleColumn totalRevenue = sales.multiply(averagePrice) // Nhân các cột
.setName("Total_Revenue_Millions"); // Đặt tên cột mới
titles.map(String::toLowerCase) // Chuyển thành chữ thường
.setName("Lowercase_Titles"); // Đặt tên cột mới
titles.map(t -> t.replaceAll("\\(.*\\)", "").trim()) // Xóa ngoặc
.setName("Titles without parentheses"); // Đặt tên cột mới
Làm sạch dữ liệu bằng Java