Limpeza de Dados em 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"); // Condição para livros com vendas acima de 70M Selection highSales = books.intColumn("Sales_in_Millions").isGreaterThan(70);// Condição para livros em inglês Selection english = books.stringColumn("Language").isEqualTo("English");// Condição para livros publicados após 1950 Selection recentlyPublished = books.intColumn("First_Published").isGreaterThan(1950);// Semelhantes: .isLessThan(), .isLessThanOrEqualTo(), .isGreaterThanOrEqualTo()
// Filtrar usando uma única condição diretamente books.where(books.intColumn("Sales_in_Millions").isGreaterThan(70));// Filtrar usando uma Selection pré-definida books.where(highSales);// Combinar várias condições com .and() Table popular = books.where(highSales) // Vendas > 70M .and(english) // Idioma inglês .and(recentlyPublished); // Após 1950 .sortDescendingOn("Sales_in_Millions"); // Ordenar por Sales_in_Millions System.out.println("Livros modernos de alta receita em inglês:\n"); System.out.println(modern.select("Books", "First_Published", "Total_Revenue"));
Livros modernos de alta receita em inglês:
| 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 |
// Importar a função de média
import static tech.tablesaw.aggregate.AggregateFunctions.mean;
Table overallMean = books.summarize("Sales_in_Millions", mean).apply();
System.out.println("\nMédia geral de vendas: \n" + overallMean);
Média geral de vendas:
| Mean [Sales_in_Millions] |
|---------------------------|
| 73.03225806451614 |
// Calcular a média de vendas por idioma (ex.: inglês, francês) Table salesByLanguage = books.summarize("Sales_in_Millions", mean) // Calcular médias.by("Language"); // Agrupar por idioma // Mostrar os 5 primeiros grupos de idioma System.out.println("Média de vendas por idioma:\n"); System.out.println(salesByLanguage.first(5));
Média de vendas por idioma:
| 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;
// Filtrar e resumir vendas por idioma Table bestsellersByLanguage = books // Selecionar livros após 1950 .where(books.intColumn("First_Published").isGreaterThan(1950))// Calcular soma das vendas .summarize("Sales_in_Millions", sum)// Agrupar resultados por idioma .by("Language"); System.out.println("Best-sellers modernos (pós-1950) por idioma:\n"); System.out.println(bestsellersByLanguage);
Best-sellers modernos (pós-1950) por idioma:
| Language | Sum [Sales_in_Millions] |
|------------|-------------------------|
| English | 1071 |
| Portuguese | 65 |
| Spanish | 50 |
| Italian | 50 |
books.where(sales.isGreaterThan(150)); // Filtragem básica com uma condição
books.where(highSales.and(english)); // Várias condições - combine com .and()
// Agregação básica
books.summarize("Sales_in_Millions", mean) // Calcular médias
.by("Language"); // Agrupar por idioma
// Operações combinadas
books.where(books.intColumn("First_Published").isGreaterThan(1950)) // Filtrar
.summarize("Sales_in_Millions", mean) // Depois resumir
.by("Language"); // Por fim agrupar
Limpeza de Dados em Java