How can I get the integer location of a key to a pandas index as quickly as possible?
eg, 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
The query function seams more efficient than the loc function. DF2: 2K records x 6 columns. The loc function seams much more efficient than the query function.
If you'd like to select rows based on integer indexing, you can use the . iloc function. If you'd like to select rows based on label indexing, you can use the . loc function.
Use get_loc, it was made for this purpose!
df.index.get_loc('bob')
Not sure if it 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
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