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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With