Pulizia dei dati in 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"); // Condizione: libri con vendite > 70M Selection highSales = books.intColumn("Sales_in_Millions").isGreaterThan(70);// Condizione: libri in inglese Selection english = books.stringColumn("Language").isEqualTo("English");// Condizione: pubblicati dopo il 1950 Selection recentlyPublished = books.intColumn("First_Published").isGreaterThan(1950);// Simili: .isLessThan(), .isLessThanOrEqualTo(), .isGreaterThanOrEqualTo()
// Filtra con una singola condizione books.where(books.intColumn("Sales_in_Millions").isGreaterThan(70));// Filtra con una Selection predefinita books.where(highSales);// Combina più condizioni con .and() Table popular = books.where(highSales) // Vendite > 70M .and(english) // Lingua inglese .and(recentlyPublished); // Dopo il 1950 .sortDescendingOn("Sales_in_Millions"); // Ordina per Sales_in_Millions System.out.println("Libri moderni ad alto fatturato in inglese:\n"); System.out.println(modern.select("Books", "First_Published", "Total_Revenue"));
Libri moderni ad alto fatturato in inglese:
| 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 |
// Importa la funzione per la media
import static tech.tablesaw.aggregate.AggregateFunctions.mean;
Table overallMean = books.summarize("Sales_in_Millions", mean).apply();
System.out.println("\nMedia generale vendite: \n" + overallMean);
Media generale vendite:
| Mean [Sales_in_Millions] |
|---------------------------|
| 73.03225806451614 |
// Calcola le vendite medie per lingua (es. inglese, francese, ecc.) Table salesByLanguage = books.summarize("Sales_in_Millions", mean) // Calcola la media.by("Language"); // Raggruppa per lingua // Mostra le prime 5 lingue System.out.println("Vendite medie per lingua:\n"); System.out.println(salesByLanguage.first(5));
Vendite medie per lingua:
| 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;
// Filtra e riassumi le vendite per lingua Table bestsellersByLanguage = books // Seleziona libri dopo il 1950 .where(books.intColumn("First_Published").isGreaterThan(1950))// Calcola il totale vendite .summarize("Sales_in_Millions", sum)// Raggruppa per lingua .by("Language"); System.out.println("Bestseller moderni (post-1950) per lingua:\n"); System.out.println(bestsellersByLanguage);
Bestseller moderni (post-1950) per lingua:
| Language | Sum [Sales_in_Millions] |
|------------|-------------------------|
| English | 1071 |
| Portuguese | 65 |
| Spanish | 50 |
| Italian | 50 |
books.where(sales.isGreaterThan(150)); // Filtro base con una condizione
books.where(highSales.and(english)); // Più condizioni: combina con .and()
// Aggregazione base
books.summarize("Sales_in_Millions", mean) // Calcola la media
.by("Language"); // Raggruppa per lingua
// Operazioni combinate
books.where(books.intColumn("First_Published").isGreaterThan(1950)) // Filtra
.summarize("Sales_in_Millions", mean) // Poi riassumi
.by("Language"); // Infine raggruppa
Pulizia dei dati in Java