Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get indexes of multiple pandas columns by names

I want to get the numerical indexes of a selection of pandas dataframe columns.

With one column it's very simple:

nonzero(df.columns.values == 'conditionA')

but with multiple elements? I have something that works but is verbose and hugly:

df = pd.DataFrame(columns=['conditionF', 'conditionB', 'conditionA', 'conditionD', 'conditionC'])

cols_to_find = ['conditionA', 'conditionB', 'conditionC']
[i for i in range(len(df.columns.values)) if df.columns.tolist()[i] in cols_to_find ]

Better ideas?

like image 284
Gioelelm Avatar asked Apr 08 '14 10:04

Gioelelm


1 Answers

This works, and also preserves order:

[df.columns.get_loc(col) for col in cols_to_find]
[2, 1, 4]

List comprehensions are your friend.

like image 195
smci Avatar answered Oct 18 '22 23:10

smci