CSV is a plain-text way to represent a two-dimensional table: file rows correspond to data rows, and commas separate columns by default. Because text files can be read and edited without special tools, CSV is a common format for downloading datasets and sharing analysis results. Its apparent simplicity hides variation: CSV has no formal specification, and delimiter, headers, indexes, and other conventions can differ between files. section 3.0
In pandas, `pd.read_csv` creates a new DataFrame from CSV input, while `df.to_csv` writes a DataFrame as CSV. Reading is deliberately configurable so that the parser can match the file rather than assume every producer made the same choices. section 3.0
Table 3.1 What you need to know (view table figure)
Use this quick reference to distinguish CSV input with `pd.read_csv` from CSV output with `df.to_csv`, alongside related data-cleaning and analysis operations. section 3.0
Start by inspecting the file’s shape and conventions, then set the arguments that describe it:
- Set `sep` when fields use a delimiter other than the default comma, such as a tab. section 3.0
- Choose an index column, restrict imported columns with `usecols`, and specify whether a header row is present. section 3.0
- Ignore comments and blank lines, supply column names, and choose an index when importing a colon-delimited Unix passwd-style file. section 3.12
- Load compressed `.csv.gz` files directly when pandas can automatically handle gzip compression. section 13.3
Investigate unusual taxi rides
Load only `passenger_count`, `trip_distance`, `payment_type`, and `total_amount` from a January 2019 taxi CSV. With the resulting DataFrame, count rides with more than eight or zero passengers, cash rides costing over $1,000, negative totals, and rides whose distance is below average while cost is above average. Selecting columns during import keeps the task focused on the fields needed for the questions. section 3.1
Compare two periods from separate files
Read July 2019 and July 2020 taxi CSV files, retain passenger count, total amount, and payment type, and attach a `year` value identifying each file. Combine the data to compare ride counts, total collections, multi-passenger trips, and cash payments across the two years. section 3.5
CSV files can also be inputs to larger analyses. Load separate institutional and fields-of-study datasets, selecting the columns needed for each, then combine DataFrames when a question needs facts from both sources. Import choices therefore shape later grouping, joining, memory use, and visualization work. section 13.3 section 13.2














