The most efficient way to get integer key index in pandas

How can I get the integer key location for the pandas index as quickly as possible?

for example, given pd.DataFrame(data=np.asarray([[1,2,3],[4,5,6],[7,8,9]]), index=['alice', 'bob', 'charlie'])

what is the fastest way to go from "bob" to 1

+4
source share
2 answers

Use get_loc , this was done for this purpose!

 df.index.get_loc('bob') 
+3
source

Not sure if this is the fastest, but you can use index.get_loc :

 df = pd.DataFrame(data=np.asarray([[1,2,3],[4,5,6],[7,8,9]]), index=['alice', 'bob', 'charlie']) print(df.index.get_loc("bob")) 1 
+2
source

All Articles