Often you may want to select the columns of a pandas DataFrame based on their index value.
If you’d like to select columns based on integer indexing, you can use the .iloc function.
If you’d like to select columns based on label indexing, you can use the .loc function.
This tutorial provides an example of how to use each of these functions in practice.
Example 1: Select Columns Based on Integer Indexing
The following code shows how to create a pandas DataFrame and use .iloc to select the column with an index integer value of 3:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'B'], 'points': [11, 7, 8, 10, 13, 13], 'assists': [5, 7, 7, 9, 12, 9], 'rebounds': [11, 8, 10, 6, 6, 5]}) #view DataFrame df team points assists rebounds 0 A 11 5 11 1 A 7 7 8 2 A 8 7 10 3 B 10 9 6 4 B 13 12 6 5 B 13 9 5 #select column with index position 3 df.iloc[:, 3] 0 11 1 8 2 10 3 6 4 6 5 5 Name: rebounds, dtype: int64
We can use similar syntax to select multiple columns:
#select columns with index positions 1 and 3 df.iloc[:, [1, 3]] points rebounds 0 11 11 1 7 8 2 8 10 3 10 6 4 13 6 5 13 5
Or we could select all columns in a range:
#select columns with index positions in range 0 through 3 df.iloc[:, 0:3] team points assists 0 A 11 5 1 A 7 7 2 A 8 7 3 B 10 9 4 B 13 12 5 B 13 9
Example 2: Select Columns Based on Label Indexing
The following code shows how to create a pandas DataFrame and use .loc to select the column with an index label of ‘rebounds’:
import pandas as pd #create DataFrame df = pd.DataFrame({'team': ['A', 'A', 'A', 'B', 'B', 'B'], 'points': [11, 7, 8, 10, 13, 13], 'assists': [5, 7, 7, 9, 12, 9], 'rebounds': [11, 8, 10, 6, 6, 5]}) #view DataFrame df team points assists rebounds 0 A 11 5 11 1 A 7 7 8 2 A 8 7 10 3 B 10 9 6 4 B 13 12 6 5 B 13 9 5 #select column with index label 'rebounds' df.loc[:, 'rebounds'] 0 11 1 8 2 10 3 6 4 6 5 5 Name: rebounds, dtype: int64
We can use similar syntax to select multiple columns with different index labels:
#select the columns with index labels 'points' and 'rebounds' df.loc[:, ['points', 'rebounds']] points rebounds 0 11 11 1 7 8 2 8 10 3 10 6 4 13 6 5 13 5
Or we could select all columns in a range:
#select columns with index labels between 'team' and 'assists' df.loc[:, 'team':'assists'] team points assists 0 A 11 5 1 A 7 7 2 A 8 7 3 B 10 9 4 B 13 12 5 B 13 9
Example 3: Using Negative Indexing with .iloc
You can also use negative indices with .iloc to select columns from the end of the DataFrame:
#select the last column with negative indexing df.iloc[:, -1] 0 11 1 8 2 10 3 6 4 6 5 5 Name: rebounds, dtype: int64
This is quite useful for selecting multiple columns from the end:
#select the last two columns df.iloc[:, -2:] assists rebounds 0 5 11 1 7 8 2 7 10 3 9 6 4 12 6 5 9 5
Example 4: Column Selection with Boolean Indexing
Another approach for selecting columns is to use boolean conditions. This is especially useful when you need to filter columns based on their names or properties:
#create list of columns to select cols_to_select = ['team', 'rebounds'] #select columns from list df.loc[:, df.columns.isin(cols_to_select)] team rebounds 0 A 11 1 A 8 2 A 10 3 B 6 4 B 6 5 B 5
We can also use pattern matching with string methods to select columns:
#select all columns that contain the letter 's' df.loc[:, df.columns.str.contains('s')] points assists rebounds 0 11 5 11 1 7 7 8 2 8 7 10 3 10 9 6 4 13 12 6 5 13 9 5
Wrapping Up
In pandas, selecting columns by index offers flexibility in data manipulation. The .iloc function works with integer positions, making it ideal for sequential data operations, while .loc uses labels for more intuitive data access. Additional techniques like negative indexing and boolean filtering expand your options for precise column selection.
The method you choose depends on your specific needs:
- Use .iloc when you know the exact position of columns
- Use .loc when working with column names
- Apply boolean indexing for filtering columns based on conditions
- Employ negative indexing to select columns from the end of your DataFrame
Related: Pandas loc vs. iloc: What’s the Difference?
Additional Resources
The following tutorials explain how to perform other common operations in pandas:
How to Group By Index in a Pandas DataFrame
How to Select Rows by Index in a Pandas DataFrame
How to Get Row Numbers in a Pandas DataFrame
How to Drop the Index Column in a Pandas DataFrame
Thanks for the help. The iloc and loc functions are just what I needed to improve my program. Thanks!
You are very welcome, Evan!