Select the First N Columns in pandas: Methods, Tradeoffs and Pitfalls
How to select the first 10 (or any N) columns of a pandas DataFrame with iloc, column slicing or usecols, what each approach changes, and when it can mislead you.
Keeping only the leading columns of a table is one of the most common trims in a delimited-data workflow. In pandas, the most direct answer is positional slicing with iloc, but there are a few alternatives, and each one makes different assumptions about your column labels and file layout. This review covers what each method does, when it is the right fit, and where the result may not be what you expected.
Short answer
first_ten = df.iloc[:, :10]
The colon before the comma means all rows; the slice after it means columns at positions 0 through 9. Change 10 to any N. Python slicing never raises an error when N is larger than the number of columns, so a DataFrame with only six columns simply returns all six.
Is positional selection suitable for your use?
Selecting by position is a good fit when column order is fixed and meaningful, such as a known export format, a quick inspection of a wide file, or a pipeline step that always receives the same layout. Its main benefit is that it works without knowing or typing any column names, and it behaves predictably even when labels are duplicated or hierarchical.
The decisive tradeoff is that position carries no meaning of its own. If an upstream system adds, removes or reorders a column, iloc will silently return a different set of fields with no error. When the columns you need have stable names, selecting by name is the safer choice for anything that runs repeatedly or feeds a report.
wanted = ['id', 'date', 'region', 'amount']
subset = df[wanted]
Name-based selection fails loudly with a KeyError if a column is missing, which is usually preferable to quietly reading the wrong data.
Ways to select the first N columns
| Method | Selects by | Notes |
| df.iloc[:, :N] | Position | Most direct; unaffected by duplicate or MultiIndex labels |
| df[df.columns[:N]] | Labels taken from the first N positions | Can return more than N columns if those labels are duplicated elsewhere |
| df.loc[:, df.columns[:N]] | Labels taken from the first N positions | Same duplicate-label caveat as above |
| pd.read_csv(path, usecols=range(N)) | Position at load time | Never loads the other columns, which can save memory on wide files |
If you only need the names rather than the data, df.columns[:N].tolist() returns them as a plain list, which is handy for logging or for building a name-based selection you can then pin down in code.
Loading only the first N columns from a file
import pandas as pd
df = pd.read_csv('data.csv', usecols=range(10))
When the source is a large delimited file and you never need the remaining columns, restricting them at read time avoids parsing and storing data you will discard. Integers in usecols refer to positions in the file, not to labels in the header row.
A small worked example
import pandas as pd
df = pd.DataFrame({'a': [1, 2], 'b': [3, 4], 'c': [5, 6], 'd': [7, 8]})
print(df.iloc[:, :3].columns.tolist())
# ['a', 'b', 'c']
What the selection actually changes
Slicing columns does not alter the cell values, the row index or the original DataFrame. It produces a new DataFrame object containing a subset of the existing columns in their current order. Nothing is reordered, renamed or converted.
Two points are easy to confuse here. First, 'first' means first in the DataFrame's current column order, which reflects the file header or any earlier reordering, not alphabetical order. Second, df.head(10) is a different operation entirely: it returns the first ten rows, not columns.
Whether the result shares memory with the original has changed across pandas versions. Older releases could return a view and raise a SettingWithCopyWarning if you then assigned values to the subset, while newer releases with Copy-on-Write behavior treat the result as independent for modification purposes. If you plan to edit the subset, calling .copy() makes the intent explicit regardless of version.
subset = df.iloc[:, :10].copy()
subset['amount'] = subset['amount'].round(2)
Pitfalls that change the meaning of the result
- Shifted fields in the source file: if some rows contain an unquoted delimiter or the file has a stray leading index column, positions no longer line up with the intended fields, and the first N columns may not be the ones you think.
- Duplicate column labels: df[df.columns[:N]] looks up labels, so a repeated name later in the table is pulled in too. Use iloc when labels are not unique.
- Saved index columns: a CSV written earlier with its index often starts with an unnamed column, which consumes one of your N positions. Read it with index_col=0 or drop it first.
- Off-by-one intent: :10 means positions 0 to 9. To get the single tenth column, use df.iloc[:, 9]; to drop the last N columns, use df.iloc[:, :-N].
Will the subset stay usable after export or sharing?
Once written back to a delimited file, the subset is ordinary text and copies like any other CSV. The main surprise is that DataFrame.to_csv writes the row index as an extra first column by default, so a ten-column selection becomes eleven columns in the file. Pass index=False if the index carries no information.
df.iloc[:, :10].to_csv('first_ten.csv', index=False)
Because the selection is positional, anyone receiving the file has no record of which columns were dropped or why. For shared or recurring outputs, keeping the explicit list of retained column names alongside the code or in a note makes the trim reproducible and easier to review.
Evidence and gaps
The behavior described here reflects documented pandas and Python slicing semantics. No benchmarks or version-specific tests were run for this article, so memory savings from usecols and the exact view-versus-copy behavior on your installation should be confirmed against your pandas version and data before relying on them in production.