liveBook · Manning

Read_csv

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

read_csv is a pandas function that parses a CSV file or compatible data source into a DataFrame.

`pd.read_csv` creates a new DataFrame by parsing CSV input or another delimited, CSV-like source. It is a top-level pandas function because it creates data from external contents rather than modifying an existing Series or DataFrame. A comma is the default field separator, but real files often need parsing instructions before their rows and columns have the intended shape. section 1.23 section 3.0

Start with only the columns needed

A taxi-rides file has millions of rows, so load the four columns needed for an analysis rather than every available field. `usecols` accepts column names or numeric positions; names are often easier to read. The returned DataFrame can then be filtered with boolean conditions and `.loc`. `df = pd.read_csv('taxi.csv', usecols=['passenger_count', 'trip_distance', 'payment_type', 'total_amount'])` section 3.2 section 3.1

Treat input settings as part of understanding the data. `sep` chooses a delimiter, `header` determines where column labels come from, `index_col` promotes a column to the index, and `usecols` limits imported fields. Examine a data dictionary when available: it can identify columns, meanings, ranges, and types before pandas guesses how to load them. section 3.0

pd.read_csv('mydata.csv',           #1
    sep='\t',                       #2
    index_col='w',                  #3
         usecols=['w', 'x', 'z'],   #4
    header=0)                       #5

This call combines separator, index, selected columns, and header settings in one import. section 3.0

Parse a non-comma file deliberately

A Unix passwd file is colon-delimited, includes comment and blank lines, and has no header row. Supply `sep=':'`, `comment='#'`, `header=None`, and column `names`; then use `index_col` when the username should identify rows. Blank lines are ignored by default. This same pattern applies whenever an ordinary-looking text file has a consistent field structure. section 3.12 section 3.13

When a file has one data column, `read_csv` still returns a one-column DataFrame. Calling `.squeeze()` converts that result to a Series, which can be counted or summarized with methods such as `value_counts`. If the first line is data rather than a label, use `header=None`; pandas infers integer dtype for integer values in the passenger-count example. section 1.22 section 1.23

Read the latest CSV from a URL

The first argument can be a filename, Path object, readable file-like object, or URL. Reading a CSV URL works like reading a local file, including `header`, `names`, and `usecols` settings. For a URL needing authentication that cannot be placed in the URL, retrieve it separately and pass a `StringIO` object containing the response. section 3.16 section 3.17

For larger or irregular inputs, make tradeoffs explicit. Restricting columns can substantially reduce memory use; `low_memory=False` lets pandas use more memory while inferring mixed column types. Compressed `.csv.gz` files can be passed directly. Date columns can be parsed with `parse_dates` and immediately made the index with `index_col`, preparing time-based analysis. section 8.2 section 13.3 section 11.6

filename = '../data/chicago,il.csv'
df = pd.read_csv(filename,
                 usecols=[0, 1,2],
                 header=0,
                 names=['date_time','mintemp', 'maxtemp'],
                parse_dates=['date_time'],
                index_col=['date_time'])

This weather import selects columns, renames them, parses a datetime column, and makes it the DataFrame index. section 11.6

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

How deep it goes
Explains practical parsing choices including delimiters, headers, comments, names, URLs, memory settings, dates, and indexes without cataloging all parameters.
Examples
Uses New York taxi rides, Unix passwd records, Bitcoin URL data, surveys, compressed college files, and city weather CSVs.
What it assumes
Assumes familiarity with pandas DataFrames and Series while introducing file-specific parsing settings through exercises.
What you can do after
Apply it via pandas to import local, compressed, remote, and file-like CSV inputs with settings matched to data shape.
How it connects
Connects DataFrame creation to Series, squeeze, value_counts, boolean indexing, loc, MultiIndex, groupby, and visualization.

Also explained in

Compare how 5 books explain read_csv →

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.