Get column-index of a specific column-name e.g. Address
should return 2 (you can assume the column exists and is unique). (Yes I know you shouldn't need to do this in general, this is only for debug while coding)
df = pd.DataFrame(np.random.rand(3,5), columns = ['Name','Age','Address','City','Zip'])
Related questions:
- this is the inverse of Retrieve name of column from its Index in Pandas , but harder
- and a simpler version of Get indexes of multiple pandas columns by names, where returning cols in the right order is tricky
- also not looking for case-insensitive or regex matches on colname, although if you want to show that, you can
- the search "pandas get column index from name" gives a very jumbled bag of 198 results.
- someday we will make all the above jumble canonical. (Then again, someday we'll colonize Mars)
- oh dear, I just found out the following previous asking, which could not be found by keyword search, probably because they said 'retrieving' Retrieving column index from column name in python pandas