🎓 A1Learner
🔍
0 of 50 lessons completed

Reading Options

The read_csv function takes options that change how it reads a file. An option is a named setting passed after the file.

A different separator

Some files use semicolons instead of commas. The sep option tells read_csv which mark splits the values.

import io
import pandas as pd

small_csv = """product;units
Tea;2
Coffee;1
Mug;3"""
df = pd.read_csv(io.StringIO(small_csv), sep=";")
print(df)

No header row

The header option set to None means no row holds column names. Pandas then numbers the columns from 0.

import io
import pandas as pd

small_csv = """product,units
Tea,2
Coffee,1
Mug,3"""
df = pd.read_csv(io.StringIO(small_csv), header=None)
print(df)

Read only some columns

The usecols option lists the columns to keep. The other columns are left out.

import io
import pandas as pd

small_csv = """order_id,product,units,price
101,Tea,2,4.50
102,Coffee,1,6.00
103,Mug,3,12.00"""
df = pd.read_csv(io.StringIO(small_csv), usecols=["product", "units"])
print(df)

A column as row labels

The index_col option uses one column as the row labels. The order_id values move to the left edge.

import io
import pandas as pd

small_csv = """order_id,product,units
101,Tea,2
102,Coffee,1
103,Mug,3"""
df = pd.read_csv(io.StringIO(small_csv), index_col="order_id")
print(df)

Example

import io
import pandas as pd

SALES_CSV = """order_id,date,product,category,units,price,region
101,2026-01-05,Tea,Drink,2,4.50,North
102,2026-01-06,Coffee,Drink,1,6.00,South
103,2026-01-07,Mug,Home,3,12.00,East
104,2026-01-08,Notebook,Office,2,8.50,West
105,2026-01-09,Pen,Office,5,2.00,North
106,2026-01-12,Lamp,Home,1,24.00,South
107,2026-01-13,Tea,Drink,4,4.50,East
108,2026-01-14,Coffee,Drink,2,6.00,West
109,2026-01-15,Mug,Home,1,12.00,North
110,2026-01-16,Notebook,Office,3,8.50,South
111,2026-01-19,Pen,Office,2,2.00,East
112,2026-01-20,Lamp,Home,2,24.00,West"""

sales = pd.read_csv(io.StringIO(SALES_CSV), index_col="order_id", usecols=["order_id", "product", "units"])
print(sales)

Key points

  • Use sep to set the separator between values.
  • The default separator is a comma.
  • Use header to pick which row holds the column names.
  • Use index_col to use one column as the row labels.
  • Use usecols to read only some columns.
  • Options go inside the read_csv call, after the file.

In this example, the sales CSV is read with two options at once. The usecols option keeps only order_id, product, and units. The index_col option uses order_id as the row labels. The print shows twelve rows with two columns.

Note

The row labels sit on the left of the table, in place of the plain row numbers.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Python Playground

Exercise

Which option reads only some columns from a CSV file?