Skillcomb.com

Select Pandas columns by index range



When working with Pandas DataFrames, we often need to select specific columns based on their index positions.

In case you are preprocessing data for machine learning, visualizing, or cleaning your dataset, selecting columns by index range is a powerful and efficient technique.

Let’s see how to select columns by index in Pandas using multiple methods such as .iloc.loc, column slicing, and NumPy indexing.

Sample DataFrame

Before we dive into the methods, let’s create a sample DataFrame to work with:

import pandas as pd
# Sample DataFrame
data = {
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [25, 30, 35, 40],
    'City': ['New York', 'Los Angeles', 'Chicago', 'Houston'],
    'Salary': [70000, 80000, 90000, 100000],
    'Department': ['HR', 'Engineering', 'Marketing', 'Finance']
}
df = pd.DataFrame(data)
print(df)

Method 1: Using iloc to Select Columns by Index Range

The .iloc accessor allows you to select columns by their integer index positions.

# Select columns from index 1 to 3 (Age, City, Salary)

# : selects all rows
# 1:4 selects columns from index 1 up to (but not including) 4

subset = df.iloc[:, 1:4]
print(subset)

Method 2: Using Column Indexing with df.columns

You can also get column names from the df.columns array and slice them based on index.

# Select column names from index 1 to 3
selected_columns = df.columns[1:4]
# Use these column names to select from DataFrame
subset = df[selected_columns]
print(subset)

This is especially useful when working dynamically with column indices.


Method 3: Using NumPy Indexing with .iloc

Combining Pandas with NumPy for flexible column selection:

import numpy as np
# Create an array of column indices
column_indices = np.arange(1, 4)
# Use iloc with numpy indexing
subset = df.iloc[:, column_indices]
print(subset)

This method is handy when your index range is generated programmatically.


Method 4: Select Specific Column Indexes (Not Range)

If you want to select non-continuous column indexes:

# Select only columns at index 0, 2, and 4
subset = df.iloc[:, [0, 2, 4]]
print(subset)

This allows for more custom column selection by position.


Method 5: Using df.loc with Column Names from Index

Although .loc uses labels (not indexes), you can still select by index first and then pass the labels.

# Get column labels using index range
column_labels = df.columns[1:4]
# Use loc with the column labels
subset = df.loc[:, column_labels]
print(subset)

This is similar to method 2 but makes use of loc.


Summary

MethodApproachCode ExampleilocIndex-based slicingdf.iloc[:, 1:4]df.columnsGet column names by indexdf[df.columns[1:4]]NumPyUse array of indexesdf.iloc[:, np.arange(1,4)]Specific indexesNon-sequentialdf.iloc[:, [0, 2, 4]]loc + df.columnsLabel-baseddf.loc[:, df.columns[1:4]]

Important Tips:

  • Always remember Python uses zero-based indexing.
  • iloc is exclusive of the end index ([start:stop)).
  • loc works with column labels, not index positions

Frequently Asked Questions — Select Pandas Columns by Index Range

How do I select columns by index range in Pandas?

Use df.iloc[:, start:end] to select columns by position. Example:

import pandas as pd
df = pd.DataFrame({'A':[1,2],'B':[3,4],'C':[5,6],'D':[7,8]})
df.iloc[:, 1:3]   # selects columns B and C

Does iloc include the ending index when selecting columns?

No, iloc slicing in Pandas is end-exclusive. df.iloc[:, 1:3] selects index 1 and 2, but not 3.

How do I select all columns from a given index to the end?

Omit the ending index: df.iloc[:, 2:] selects all columns starting from index 2 to the last column.

Can I select non-contiguous column ranges by index?

Yes — use a list of indexes instead of a range: df.iloc[:, [0, 2, 4]] selects specific columns.

How do I get column names from an index range in Pandas?

Use df.columns[start:end] to get the column labels for that range. Example: df.columns[1:3]

How to select columns by index range and keep as DataFrame?

iloc always returns a DataFrame when you slice with :. Example: df.iloc[:, 1:3].

How to dynamically select a range of columns based on variable indexes?

Use variables inside iloc slicing: start, end = 1, 4; df.iloc[:, start:end].

Can I rename columns selected by index range?

Yes — get the range with df.columns[start:end] and pass to rename() or reassign new names.

What happens if I select an index range out of bounds?

Pandas automatically adjusts to valid indexes — it won’t raise an error unless all indexes are invalid.

Which method is best to select columns by index range in Pandas?

df.iloc[:, start:end] is the recommended and fastest method for selecting columns by numeric index range.

Related Post