Initializing playground…
← Back to roadmap

📄 readCsv & toCsv — Interactive Playground

Parse CSV text into a DataFrame with automatic dtype inference, and serialize any DataFrame back to CSV with full formatting control. Mirrors pandas.read_csv() and pandas.DataFrame.to_csv().
Edit any code block below and press ▶ Run (or Ctrl+Enter) to execute it live in your browser.

1 · Parse a CSV string

The simplest call is readCsv(text). The first row is the header, subsequent rows are data. Column dtypes are inferred automatically.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

2 · Missing values (NA)

Empty fields, NA, NaN, null, None, and several other sentinel strings are automatically converted to null. Pass extra strings via naValues.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

3 · Quoted fields & custom separator

Fields containing the separator, quotes, or newlines can be wrapped in double-quotes. Use sep to change the delimiter (tab, semicolon, pipe, etc.).

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

4 · Index column

Set indexCol to a column name or position to use that column as the row index instead of the default RangeIndex.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

5 · Limiting rows

Use nRows to read only the first N data rows, and skipRows to skip rows at the start.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

6 · Serialize with toCsv

toCsv(df) converts a DataFrame back to a CSV string. Control index inclusion, header, separator, and NA representation.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

7 · Round-trip

A DataFrame serialized with toCsv can be reconstructed with readCsv without data loss.

TypeScript
Click ▶ Run to execute
Ctrl+Enter to run · Tab to indent

API Reference

Parse CSV text into a DataFrame or serialize a DataFrame back to CSV. All options are optional — sensible defaults are provided.

// Parse CSV text → DataFrame
readCsv(text: string, opts?: {
  sep?:       string,            // default ","
  naValues?:  readonly string[], // extra NA sentinel strings
  indexCol?:  string | number,   // column to use as row index
  nRows?:     number,            // max data rows to read
  skipRows?:  number,            // rows to skip at start
}): DataFrame

// Serialize DataFrame → CSV text
toCsv(df: DataFrame, opts?: {
  sep?:    string,   // default ","
  index?:  boolean,  // default true — include index
  header?: boolean,  // default true — include header row
  naRep?:  string,   // default "" — NA representation
}): string