Pandas: How to Get Column Index from Column Name


You can use the following methods to get the column index value from a column name in pandas:

Method 1: Get Column Index for One Column Name

df.columns.get_loc('this_column')

Method 2: Get Column Index for Multiple Column Names

cols = ['this_column', 'that_column']

[df.columns.get_loc(c) for c in cols if c in df]

The following examples show how to use each method in practice with the following pandas DataFrame:

import pandas as pd

#create DataFrame
df = pd.DataFrame({'store': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B'],
                   'sales': [18, 10, 14, 13, 19, 24, 25, 29],
                   'returns': [1, 2, 2, 3, 2, 3, 5, 4],
                   'recalls': [0, 0, 2, 1, 1, 2, 0, 1]})
#view DataFrame
print(df)

  store  sales  returns  recalls
0     A     18        1        0
1     A     10        2        0
2     A     14        2        2
3     A     13        3        1
4     B     19        2        1
5     B     24        3        2
6     B     25        5        0
7     B     29        4        1

Example 1: Get Column Index for One Column Name

The following code shows how to get the column index value for the column with the name ‘returns’:

#get column index for column with the name 'returns'
df.columns.get_loc('returns')

2

The column with the name ‘returns’ has a column index value of 2.

Note: Column index values start at 0 in Python. Thus, since ‘returns’ is the third column in the DataFrame, it has an index value of 2.

Example 2: Get Column Index for Multiple Column Names

The following code shows how to get the column index value for several columns in the DataFrame:

#define list of columns to get index for
cols = ['store', 'returns', 'recalls']

#get column index for each column in list
[df.columns.get_loc(c) for c in cols if c in df]

[0, 2, 3]

From the output we can see:

  • The column with the name ‘store’ has a column index value of 0.
  • The column with the name ‘returns’ has a column index value of 2.
  • The column with the name ‘recalls’ has a column index value of 3.

Note: You can find the complete documentation for the pandas get_loc() function here.

Additional Resources

The following tutorials explain how to perform other common tasks in pandas:

How to Get Cell Value from Pandas DataFrame
How to Rename Index in Pandas DataFrame
How to Sort Columns by Name in Pandas

Leave a Reply

Your email address will not be published.