pandas get index of column code example

Example 1: get column number in dataframe pandas

# PANDAS: get column number from colomn name
dataframe.columns.get_loc("<col_name>")

Example 2: find index of pandas column

In [45]: df = DataFrame({"pear": [1,2,3], "apple": [2,3,4], "orange": [3,4,5]})

In [46]: df.columns
Out[46]: Index([apple, orange, pear], dtype=object)

In [47]: df.columns.get_loc("pear")
Out[47]: 2

Example 3: finding the index of an element in a pandas df

In [48]: a
Out[48]: 
   c1  c2
0   0   1
1   2   3
2   4   5
3   6   7
4   8   9

In [49]: a.c1[a.c1 == 8].index.tolist()
Out[49]: [4]

Example 4: finding the index of an item in a pandas df

print(df[df['Name']=='Donna'].index.values)

Example 5: get index number pandas dataframe

df.index[df['BoolCol'] == True].tolist()