Pembersihan Data di 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"); // Kondisi untuk buku dengan penjualan > 70M Selection highSales = books.intColumn("Sales_in_Millions").isGreaterThan(70);// Kondisi untuk buku berbahasa Inggris Selection english = books.stringColumn("Language").isEqualTo("English");// Kondisi untuk buku terbit setelah 1950 Selection recentlyPublished = books.intColumn("First_Published").isGreaterThan(1950);// Serupa: .isLessThan(), .isLessThanOrEqualTo(), .isGreaterThanOrEqualTo()
// Saring dengan satu kondisi langsung books.where(books.intColumn("Sales_in_Millions").isGreaterThan(70));// Saring dengan Selection yang sudah didefinisikan books.where(highSales);// Gabungkan beberapa kondisi dengan .and() Table popular = books.where(highSales) // Sales > 70M .and(english) // Bahasa Inggris .and(recentlyPublished); // Setelah 1950 .sortDescendingOn("Sales_in_Millions"); // Urutkan berdasarkan Sales_in_Millions System.out.println("Buku Inggris berpendapatan tinggi modern:\n"); System.out.println(modern.select("Books", "First_Published", "Total_Revenue"));
Buku Inggris berpendapatan tinggi modern:
| 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 |
// Impor fungsi perhitungan mean
import static tech.tablesaw.aggregate.AggregateFunctions.mean;
Table overallMean = books.summarize("Sales_in_Millions", mean).apply();
System.out.println("\nRata-rata penjualan keseluruhan: \n" + overallMean);
Rata-rata penjualan keseluruhan:
| Mean [Sales_in_Millions] |
|---------------------------|
| 73.03225806451614 |
// Hitung rata-rata penjualan per bahasa (mis. Inggris, Prancis, dll.) Table salesByLanguage = books.summarize("Sales_in_Millions", mean) // Hitung mean.by("Language"); // Kelompokkan per bahasa // Tampilkan 5 bahasa pertama System.out.println("Rata-rata penjualan per bahasa:\n"); System.out.println(salesByLanguage.first(5));
Rata-rata penjualan per bahasa:
| 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;
// Saring dan ringkas penjualan per bahasa Table bestsellersByLanguage = books // Pilih buku setelah 1950 .where(books.intColumn("First_Published").isGreaterThan(1950))// Hitung total penjualan .summarize("Sales_in_Millions", sum)// Kelompokkan hasil per bahasa .by("Language"); System.out.println("Bestseller modern (pasca-1950) per bahasa:\n"); System.out.println(bestsellersByLanguage);
Bestseller modern (pasca-1950) per bahasa:
| Language | Sum [Sales_in_Millions] |
|------------|-------------------------|
| English | 1071 |
| Portuguese | 65 |
| Spanish | 50 |
| Italian | 50 |
books.where(sales.isGreaterThan(150)); // Penyaringan dasar dengan satu kondisi
books.where(highSales.and(english)); // Beberapa kondisi - gabungkan dengan .and()
// Agregasi dasar
books.summarize("Sales_in_Millions", mean) // Hitung mean
.by("Language"); // Kelompokkan per bahasa
// Operasi gabungan
books.where(books.intColumn("First_Published").isGreaterThan(1950)) // Saring
.summarize("Sales_in_Millions", mean) // Lalu ringkas
.by("Language"); // Terakhir kelompokkan
Pembersihan Data di Java