search
Search
Login
Unlock 100+ guides
menu
menu
web
search toc
close
Comments
Log in or sign up
Cancel
Post
account_circle
Profile
exit_to_app
Sign out
What does this mean?
Why is this true?
Give me some examples!
search
keyboard_voice
close
Searching Tips
Search for a recipe:
"Creating a table in MySQL"
Search for an API documentation: "@append"
Search for code: "!dataframe"
Apply a tag filter: "#python"
Useful Shortcuts
/ to open search panel
Esc to close search panel
to navigate between search results
d to clear all current filters
Enter to expand content preview
icon_star
Doc Search
icon_star
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Navigate to

Pandas Index | get_loc method

schedule Aug 12, 2023
Last updated
local_offer
PythonPandas
Tags
mode_heat
Master the mathematics behind data science with 100+ top-tier guides
Start your free 7-days trial now!

Pandas Index.get_loc(~) method returns the location of the given value in the source Index.

Parameters

1. key | label

The value you want to know the location of.

2. methodlink | None or string | optional

How to deal with the case when key is not present in the source Index:

Method

Description

None

Throw an error.

"pad" or "ffill"

Return the integer index of the previous index value.

"backfill" or "bfill"

Return the integer index of the next index value.

"nearest"

Return the integer index of the nearest index value.

As a tie-breaker, the larger index value will take precedence.

By default, method=None.

WARNING

Your Index must be monotonic, otherwise specifying method will throw an error.

3. tolerancelink | int or float | optional

The maximum difference allowed between the key and the selected index value:

abs(index[loc] - key) <= tolerance

This is only relevant if method is not None. By default, no tolerance is set.

Return Value

The return type depends on the following cases:

Case

Return Type

If a single match is found.

int

If multiple matches are found and

the source Index is monotonically increasing/decreasing

slice

Otherwise

a Numpy array of booleans

Examples

Single match

To get the integer index of an unique value:

idx = pd.Index(["a","b","c"])
idx.get_loc("c")
2

Here, the integer index of the value "c" is 2.

Multiple matches

When index is monotonic

To get the integer indexes of a value that is non-unique in a monotonic Index:

idx = pd.Index(["a","b","c","c"])
slice = idx.get_loc("c")
slice
slice(2, 4, None)

Since idx is monotonically increasing, the return type here is a slice object. Note that you can access the properties of slice like so:

print("start", slice.start)
print("stop", slice.stop)
print("step", slice.step)
start 2
stop 4
step None

When index is not monotonic

To get the locations of a value that is non-unique in a non-monotonic Index:

idx = pd.Index(["a","b","c","b"])
idx.get_loc("b")
array([False, True, False, True])

Here, the return type is a Numpy array of booleans where True indicates the presence of the value.

Specifying method

By default, when the supplied value does not exist in the Index, a KeyError is thrown:

idx = pd.Index([3,5,8])
idx.get_loc(6)
KeyError: 6

In such cases when the value is not present in Index, we can specify method to get the integer index of a neighbouring value. Note that our Index must be monotonic, otherwise specifying method will throw an error.

ffill

When method="ffill", then the integer index of the previous value will be returned:

idx = pd.Index([3,5,8])
idx.get_loc(7, method="ffill")
1

Here, the previous value of 7 is 5, so the integer index of 5 is returned.

bfill

When method="bfill", then the integer index of the next value will be returned:

idx = pd.Index([3,5,8])
idx.get_loc(6, method="bfill")
2

Here, the next value of 6 is 8, so the integer index of 8 is returned.

nearest

When method="nearest", then the integer index of the nearest value will be returned:

idx = pd.Index([3,5,8])
idx.get_loc(6, method="nearest")
1

Here, the nearest value to 6 is 5 (as opposed to 8), so the integer index of 5 is returned.

Specifying tolerance

When an exact match is not found, then setting tolerance imposes a maximum acceptable difference between the supplied value and the selected index value:

idx = pd.Index([3,5,8])
idx.get_loc(6, method="nearest", tolerance=0.5)
KeyError: 6

Here, we get an error because the nearest index value is 5, and the difference between 5 and the supplied value 6 is larger than the specified tolerance of 0.5.

robocat
Published by Isshin Inada
Edited by 0 others
Did you find this page useful?
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
1
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!