CSV Options That Matter

Separators, headers and the arguments you reach for on a real file.

The file is not always comma separated

sep tells read_csv what divides the fields. A tab-separated export read with the default gives you one very wide column, which is easy to spot the moment you check shape.

import pandas as pd

df = pd.read_csv("scores.tsv", sep="\t")
print(df.shape)

Headers, and files without one

header=None stops pandas taking the first row of data as your column names, and names= supplies your own.

import pandas as pd

df = pd.read_csv("raw.csv", header=None, names=["name", "score"])
print(list(df.columns))

Exercise

Try It Yourself

A tab-separated file scores.tsv is supplied with name and score columns. Write row_count(path) that reads it and returns the number of rows.

Press Run to see output

Check Your Understanding