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.api.StringColumn;
import tech.tablesaw.api.DoubleColumn;
Table books = Table.read().csv("bestsellers.csv"); for (String colName : books.columnNames()) {// Stampa nome colonna e tipo System.out.println(colName + ": " + books.column(colName).type()); }
Books: STRING
Authors: STRING
Language: STRING
First_Published: INTEGER
Sales_in_Millions: INTEGER
Average_Price: DOUBLE
// Converte IntColumn in DoubleColumn perché Tablesaw l'ha inferita come INTEGER
DoubleColumn sales = books.intColumn("Sales_in_Millions")
.asDoubleColumn();
System.out.println("Data type of sales column after conversion: " + sales.type());
Data type of sales column after conversion: DOUBLE
// È possibile convertire una DoubleColumn in IntColumn
IntColumn salesAsInt = sales.asIntColumn();
DoubleColumn averagePrice = books.doubleColumn("Average_Price"); // Calcola il ricavo totale moltiplicando due colonne DoubleColumn totalRevenue = sales.multiply(averagePrice).setName("Total_Revenue_Millions");books.addColumns(totalRevenue); System.out.println("\nMoltiplicare due colonne: Sales × Price = Revenue"); System.out.println(books.selectColumns("Books", "Sales_in_Millions", "Average_Price", "Total_Revenue_Millions") .first(4).print());
Moltiplicare due colonne: Sales * Price = Revenue
| Libri | 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 |
\\(.*\\) corrisponde alle parentesi\\( // Match parentesi aperta (con \)
.* // Qualsiasi carattere (.) zero o più volte (*)
\\) // Match parentesi chiusa (con \)
// Rimuovi traduzioni tra parentesi dai titoli
StringColumn titlesNoParentheses = titles
// Per ogni titolo, rimuovi parentesi e spazi circostanti
.map(t -> t.replaceAll("\\(.*\\)", "").trim())
// Imposta il nuovo nome colonna
.setName("Titles without parentheses");
System.out.println("Original: " + titles.get(1)); // Primo elemento della colonna
System.out.println("Cleaned: " + titlesNoParentheses.get(1));
Original: The Little Prince (Le Petit Prince)
Cleaned: The Little Prince
// Combina rimozione parentesi e conversione in minuscolo StringColumn titlesCombinedCleaning = titles.map(t -> t.replaceAll("\\(.*\\)", "") // Rimuovi parentesi.trim() // Rimuovi spazi ai bordi.toLowerCase()) // Converti in minuscolo .setName("Clean_Titles_Combined"); // Nome colonna 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(); // Tipo colonna
books.intColumn("Sales_in_Millions").asDoubleColumn(); // Converti tipo
DoubleColumn totalRevenue = sales.multiply(averagePrice) // Moltiplica colonne
.setName("Total_Revenue_Millions"); // Nome nuova colonna
titles.map(String::toLowerCase) // Converti in minuscolo
.setName("Lowercase_Titles"); // Nome nuova colonna
titles.map(t -> t.replaceAll("\\(.*\\)", "").trim()) // Rimuovi parentesi
.setName("Titles without parentheses"); // Nome nuova colonna
Pulizia dei dati in Java