Pandas Read CSV

CSV files are one of the most common data formats, and pandas' read_csv() function makes loading them effortless.

Reading CSV Files

Comma-separated values (CSV) files store tabular data as plain text, with each line representing a row and commas separating the columns. The pd.read_csv() function reads a CSV file directly into a DataFrame, automatically inferring column names and data types.

Read a CSV File

import pandas as pd

df = pd.read_csv('data.csv')
print(df)

Handling Large DataFrames With to_string()

When a DataFrame has more rows than the display limit (60 by default), printing it directly shows only the first and last five rows. Calling to_string() forces pandas to print the entire DataFrame instead.

Print the Full DataFrame

import pandas as pd

df = pd.read_csv('data.csv')
print(df.to_string())
  • sep - the character that separates fields, comma by default
  • header - which row to use as the column names
  • index_col - a column to use as the row index instead of 0, 1, 2...
  • usecols - load only a subset of the available columns
  • nrows - limit how many rows are read, useful for huge files

Configuring Pandas Display Options

Instead of calling to_string() every time, you can permanently raise the row limit for your session by changing the max_rows display option before printing.

Change the Max Rows Setting

import pandas as pd

print(pd.options.display.max_rows)
pd.options.display.max_rows = 9999

df = pd.read_csv('data.csv')
print(df)
ParameterDescription
sep / delimiterCharacter(s) that separate fields (default ',')
headerRow number(s) to use as column names
index_colColumn to use as the row index
usecolsSubset of columns to load
nrowsNumber of rows to read
Note: The default max_rows setting is 60 - pandas shows the first and last 5 rows for anything larger, so always use to_string() or adjust the display option when you need to see everything.

Exercise: Pandas Read CSV

Which function loads a CSV file into a DataFrame?