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.DoubleColumn;
import tech.tablesaw.api.StringColumn;
// Nạp tệp CSV vào bảng Tablesaw
Table books = Table.read().csv("bestsellers.csv");
// Thông tin cơ bản: số dòng, số cột, tên cột
System.out.println("Row count: " + books.rowCount());
System.out.println("Column count: " + books.columnCount());
System.out.println("Column names: " + books.columnNames());
Row count: 290
Column count: 6
Column names: [Books, Authors, Language, First_Published, Sales_in_millions]
for (String columnName : books.columnNames()) {// Lấy cột theo tên và đếm giá trị thiếu (null) int missing = books.column(columnName).countMissing();System.out.println(columnName + " missing values: " + missing); }
Books missing values: 0
Authors missing values: 0
Language missing values: 0
First_Published missing values: 0
Sales_in_millions missing values: 2
Average_Price missing values: 0
// countBy() tạo bảng mới với các giá trị duy nhất và tần suất
Table languageCounts = books.countBy("Language");
System.out.println("Phân bố ngôn ngữ:\n");
// In bảng định dạng hiển thị từng ngôn ngữ và số lượng
System.out.println(languageCounts);
Phân bố ngôn ngữ:
| Language | Count |
|------------|-------|
| English | 210 |
| French | 10 |
| Chinese | 6 |
| Portuguese | 1 |
| Spanish | 3 |
| German | 6 |
| Italian | 5 |
// Lấy cột số dưới dạng DoubleColumn để tính thống kê DoubleColumn sales = books.doubleColumn("Sales_in_millions");System.out.println("Thống kê doanh số (triệu bản):\n"); // Giá trị nhỏ nhất System.out.println("Min: " + sales.min() + " triệu"); // Giá trị lớn nhất System.out.println("Max: " + sales.max() + " triệu");// Trung bình các giá trị System.out.println("Trung bình: " + sales.mean() + " triệu"); // Độ phân tán (độ lệch chuẩn) System.out.println("Độ lệch chuẩn: " + sales.standardDeviation() + " triệu");
Thống kê doanh số (triệu bản):
Min: 10.0 triệu
Max: 600.0 triệu
Trung bình: 49.996875 triệu
Độ lệch chuẩn: 64.6846320839116 triệu
System.out.printf("Số dòng: %d, Số cột: %s", books.rowCount(), books.columnCount());
for (String colName : books.columnNames()) // Với mỗi cột
System.out.println(books.column(colName).countMissing()); // Đếm giá trị null
StringColumn language = books.stringColumn("Language"); // Lấy cột văn bản
Table langCounts = books.countBy("Language"); // Đếm từng hạng mục
// Tính thống kê cho cột số
DoubleColumn sales = books.doubleColumn("Sales_in_Millions"); // Lấy cột số
System.out.printf("Trung bình: %.1f triệu, Min: %.1f triệu, Max: %.1f triệu",
sales.mean(), sales.min(), sales.max());
Số dòng: 290, Số cột: 6
Books thiếu giá trị: 0
Authors thiếu giá trị: 0
| Language | Count |
|------------|-------|
| English | 210 |
| French | 10 |
Trung bình: 50.0 triệu, Min: 10.0 triệu, Max: 600.0 triệu
Làm sạch dữ liệu bằng Java