Importare dati in Java
Anthony Markham
VP Quant Developer
import tech.tablesaw.api.*import tech.tablesaw.api.DoubleColumnimport tech.tablesaw.api.StringColumnimport tech.tablesaw.aggregate.*
// Approccio tradizionale Java (macchinoso)
import java.util.Arrays;
import java.util.List;
List<String> names = Arrays.asList("Anna", "Bob", "Carlos");
List<Integer> ages = Arrays.asList(25, 34, 42);
// Creazione da zero
Table employees = Table.create("Employees")
.addColumns(
StringColumn.create("Name", "John", "Lisa", "Omar"),
DoubleColumn.create("Salary", 50000, 60000, 55000)
);
// Da colonne esistenti
StringColumn dept = StringColumn.create("Department",
"Sales", "Marketing", "Engineering");
Table departments = Table.create("Departments", dept);
addColumns() e create()table.shape()table.columnNames()table.structure()table.first(n), table.last(n)// Stampa dimensioni
System.out.println(data.shape()); // [righe, colonne]
[10, 4]
// Stampa nomi colonne
System.out.println(table.columnNames());
[Day, Temperature, Precipitation]
// Stampa struttura dettagliata
System.out.println(table.structure());
Struttura della tabella
Indice | Nome Colonna | Tipo Colonna |
0 | Day | STRING |
1 | Temperature | DOUBLE |
2 | Precipitation | DOUBLE |
// Anteprima delle prime tre righe
System.out.println(table.first(3));
tabella
Day | Temperature | Precipitation |
Monday | 22.5 | 0 |
Tuesday | 24 | 2.5 |
Wednesday | 23.2 | 5.2 |
table.addColumns(newColumn)// Aggiungi una nuova colonna
DoubleColumn bonus = DoubleColumn.create("Bonus", 1000, 1500, 2000);
employees = employees.addColumns(bonus);
// Rimuovi una colonna
employees = employees.removeColumns("StartDate");
// Rinomina una colonna
employees.column("Salary").setName("AnnualSalary");
// Ottieni il tipo di colonna
employees.column("Salary").type();
ColumnType.INTEGER
$$
| Metodo/Sintassi | Descrizione |
|---|---|
Table.create("TableName") |
Crea una nuova tabella con il nome dato |
StringColumn.create("ColumnName", values) |
Crea una colonna di stringhe |
table.shape() |
Restituisce le dimensioni come [righe, colonne] |
table.columnNames() |
Restituisce i nomi delle colonne nella tabella |
table.structure() |
Mostra le informazioni sulla struttura della tabella |
table.first(n) |
Restituisce le prime n righe della tabella |
table.last(n) |
Restituisce le ultime n righe della tabella |
table.addColumns(newColumn) |
Aggiunge una nuova colonna alla tabella |
Importare dati in Java