CSV processing with Tablesaw

Importing Data in Java

Anthony Markham

VP Quant Developer

Reading CSV files

  • Read CSV files using read().csv()
import tech.tablesaw.api.Table;
// Read in data
Table dataTable = Table.read().csv("data.csv");
  • Automatically detects column types, creating a structured Table object 💡
Importing Data in Java

CSV reading options

  • Use CsvReadOptions for more control
import tech.tablesaw.io.csv.CsvReadOptions;
CsvReadOptions options = CsvReadOptions.builder("data.csv")

.separator(';') // Use semicolon as delimiter
.header(true) // First row contains headers
.missingValueIndicator("N/A") // Treat "N/A" as missing data
.build();
// Load the table using the custom options Table table = Table.read().csv(options);
Importing Data in Java

Writing CSV files

  • Use write().csv() for basic CSV export
  • Imported with Table class
  • Preserves column types and structure
  • Handles special characters automatically
// Write dataTable to output.csv
dataTable.write().csv("output.csv");
Importing Data in Java

CSV writing options

  • Use CsvWriteOptions to specify options for writing
CsvWriteOptions writeOptions = CsvWriteOptions
    .builder("output.csv")

.header(true) // Include column headers
.separator(';') // Use semicolon delimiter
.quoteAlways(true) // Quote all fields
.lineEnd("\r\n") // Windows-style line endings
.build();
// Write the CSV using the custom options
Table.write().csv(writeOptions);
Importing Data in Java

CSV workflow

  • Complete workflow: read -> inspect -> process -> write
  • Non-destructive operations (creates new files) 📁
// Read CSV, modify, and write back
Table students = Table.read().csv("students.csv");

// View structure
System.out.println(students.structure());

// Save as new file
students.write().csv("students_processed.csv");
Importing Data in Java

Let's practice!

Importing Data in Java

Preparing Video For Download...