liveBook · Manning

Delimiter

As explained in Pandas Workout: 200 exercises to make you a stronger data analyst

A field separator is a character or string that delimits adjacent fields in a structured text record.

A delimiter, or field separator, marks the boundary between adjacent fields in a structured text record. In CSV data, commas separate fields by default, but files may use tabs, colons, or other characters. pandas exposes this choice through the sep argument of read_csv and to_csv. section 3.0

Parsing a colon-delimited passwd file

A Unix passwd file is a useful variation on ordinary CSV: its fields are separated by colons rather than commas. Parse it with read_csv('linux-etc-passwd.txt', sep=':', comment='#', header=None, names=['username', 'password', 'userid', 'groupid', 'name', 'homedir', 'shell'], index_col='username'). The separator makes each colon-delimited value become a separate data-frame column; comment and header options handle the file's other irregularities. section 3.12 section 3.13

When importing delimited text, check these related assumptions:

  • Use sep=',' for ordinary comma-separated data or replace the comma with the file's actual separator, such as sep='\t' or sep=':'. section 3.0 section 3.13
  • Treat the separator as one part of parsing: headers, comments, blank lines, and index columns can also determine whether the resulting data frame is usable. section 3.13
pd.read_csv('mydata.csv',           #1
    sep='\t',                       #2
    index_col='w',                  #3
         usecols=['w', 'x', 'z'],   #4
    header=0)                       #5

Inspect a compact read_csv call that combines sep with index, column-selection, and header arguments. section 3.0

How Pandas Workout: 200 exercises to make you a stronger data analyst covers it

How deep it goes
Defines the separator choice and demonstrates it within broader read_csv parsing, without examining parser internals.
Examples
Uses comma-separated files, tab-separated input, and the colon-delimited Unix passwd file.
What it assumes
Assumes familiarity with pandas data frames and read_csv, while requiring no Unix knowledge for the passwd-file exercise.
What you can do after
Apply it via pandas read_csv and to_csv to parse or write records using a file's actual field separator.
How it connects
Connects CSV files with read_csv, to_csv, headers, comments, blank lines, indexes, and data-frame columns.

Also explained in

Compare how 11 books explain delimiter →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Pandas Workout: 200 exercises to make you a stronger data analyst. The section links point to the parts of the book it draws on.