Select columns when reading a CSV file
How to read only certain columns from a CSV file, what column selection actually changes, where simple tools break on quoted fields, and how to keep the result usable when you share it.
Reading only the columns you need from a CSV file is one of the most common delimited-text tasks. It shortens output, reduces what a program has to hold in memory, and keeps unrelated or sensitive fields out of the next step. Whether it is the right move depends on two things: whether the tool you use actually understands CSV quoting, and whether the people or programs downstream still have the context they need. This review covers what column selection does, where it fails quietly, and when keeping the full file is the better choice.
Is column selection suitable for your use?
For most analysis, reporting and data-handoff tasks, yes. The main benefit is focus: you work with a smaller, predictable set of fields, and anything you leave out cannot leak into a chart, an export or a shared file. The decisive tradeoff is context. Once a column is gone from the output, nobody downstream can see it, check it or rejoin on it unless they go back to the source.
Keep the full file, or share it alongside your subset, when any of these apply:
- You need an audit trail, and reviewers may have to trace a value back to its original row.
- The receiving person or system needs a key column, such as an ID, to match records later.
- You are not yet sure which fields matter, as in early exploration or debugging.
- The file's structure is irregular, with ragged rows or repeated header names, and you have not confirmed how your tool handles it.
What column selection actually changes
Selecting columns at read time is a projection. The reader parses each row, keeps the fields you named or numbered, and discards the rest from the result. The source file on disk is not modified. Nothing is reversed, restyled or substituted, and the values you keep are passed through as parsed, subject to whatever type conversion your tool applies.
Column selection differs from hiding a column in a spreadsheet. Hiding changes only what is displayed; the data is still stored and travels with the file when you save or share it. Dropping a column during a read or export removes it from the result. If your goal is to keep a field from reaching someone else, only the second approach does that.
There are two common ways to name what you want. Selecting by header name is more robust when column order may change between file versions. Selecting by position works for files without headers, but it silently picks the wrong data if someone inserts or reorders a column.
Where simple tools get it wrong: quoted fields
CSV allows a field to contain the delimiter as long as the field is wrapped in quotes. Tools that split each line on every comma do not understand this rule. Consider this input:
order_id,customer,note,total
1001,"Lee, Dana",gift wrap,42.50
1002,Ortiz,,18.00
Asking a plain field splitter for columns 1 and 4 looks reasonable:
cut -d',' -f1,4 orders.csv
The comma inside "Lee, Dana" is treated as a delimiter, so row 1 is split into five pieces instead of four, and field 4 becomes the note rather than the total:
order_id,total
1001,gift wrap
1002,18.00
The command produces no error, and the header row looks correct. Only some data rows are wrong, so the problem is easy to miss. A CSV-aware reader parses the quotes and returns the intended values:
order_id,total
1001,42.50
1002,18.00
Line-based splitters are fine for files you control and know contain no quoted delimiters or embedded line breaks. For anything else, use a reader that implements CSV quoting rules.
Common ways to read only certain columns
| Approach | Selects by | Handles quoted fields | Notes |
| pandas read_csv with usecols | Name, position or a function | Yes | Result follows file order, not the order you list |
| Python csv.DictReader | Header name | Yes | Reads row by row; low memory use for large files |
| csvkit csvcut | Name or position | Yes | Command-line; useful in shell pipelines |
| cut or similar splitters | Position only | No | Safe only for simple, unquoted data |
pandas
import pandas as pd
df = pd.read_csv("orders.csv", usecols=["order_id", "total"])
usecols also accepts positions, such as [0, 3], or a function that receives each header name. A function is useful when headers carry stray whitespace or inconsistent capitalization:
wanted = {"order_id", "total"}
df = pd.read_csv("orders.csv", usecols=lambda c: c.strip().lower() in wanted)
pandas documents that element order in usecols is ignored, so the columns come back in the order they appear in the file. If you need a specific order, reindex after reading, for example df[["total", "order_id"]]. Naming a column that does not exist raises an error rather than returning an empty column, which is usually what you want.
Python's standard csv module
To write a subset to a new file without loading everything into memory:
import csv
fields = ["order_id", "total"]
with open("orders.csv", newline="", encoding="utf-8") as src, \
open("subset.csv", "w", newline="", encoding="utf-8") as dst:
reader = csv.DictReader(src)
writer = csv.DictWriter(dst, fieldnames=fields, extrasaction="ignore")
writer.writeheader()
writer.writerows(reader)
extrasaction="ignore" tells the writer to drop any keys that are not in fields. Unlike positional selection, the writer outputs columns in the order you list them. If the file was saved with a byte order mark, which some spreadsheet exports add, open it with encoding="utf-8-sig" so the first header name matches.
Command line
csvcut -c order_id,total orders.csv > subset.csv
csvcut is part of csvkit and parses quoted fields, so it avoids the cut problem shown above. It needs to be installed separately.
Will the result stay usable after you copy or share it?
A column subset written back out as CSV is still plain text. It copies, pastes and opens the same way as any other CSV file. Most usability problems come from how values were parsed on the way in, not from the selection itself:
- Leading zeros. If a reader converts a column such as a ZIP code or account number to a number, values like 02134 lose their leading zero. In pandas, pass dtype={"zip": str} for those columns.
- Re-quoting on output. A CSV writer will quote values that contain commas or quotes. A hand-built output, such as joining strings with commas, will not, and that breaks the file for the next reader.
- Missing headers. Selecting by position from a headerless file produces output with no column names. Add a header row before sharing so recipients know what each column means.
- Lost keys. If you drop the ID column, recipients cannot match rows back to the source. Keep at least one identifying column unless removing it is the point.
Fewer columns generally make a table easier to scan, both visually and when a screen reader announces it cell by cell. That is a general principle, not a tested result: no evidence was supplied about how a specific spreadsheet application, chat tool or assistive technology presents a given CSV file. If the output is meant for a particular destination, open it there and check that the headers, values and column alignment come through as expected.
Privacy when removing columns
Dropping columns before sharing is a sound way to limit what you disclose, provided the columns are truly removed and not just hidden. Check the saved output file itself rather than the on-screen view. If you use an online CSV tool to make the selection, its appearance and marketing language say nothing reliable about whether it stores uploaded files. Look for a stated data-handling policy, or do the selection locally with one of the approaches above.
Bottom line
Reading only certain columns is a safe, practical transformation when you use a CSV-aware reader, select by header name where possible, and protect values like leading-zero codes from type conversion. Avoid plain line splitters on data that may contain quoted commas, because they fail without warning. Keep or share the full file when downstream users need the context, keys or audit trail that a subset removes.