search
Search
Unlock 100+ guides
search toc
close
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
Doc Search
Code Search Beta
SORRY NOTHING FOUND!
mic
Start speaking...
Voice search is only supported in Safari and Chrome.
Shrink
Navigate to

# Getting index of value in Series in Pandas

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

# Series Index

## Unique case

To get the index of a value in a Series:

``` s = pd.Series([7,4,5,6], index=["a","b","c","d"])s[s == 5] # returns a Series c 5dtype: int64 ```

## Duplicate case

For the duplicate case:

``` s = pd.Series([7,4,5,5], index=["a","b","c","d"])s[s == 5] c 5d 5dtype: int64 ```

## Non-existent case

When the value you are finding does not exist, you get an empty Series:

``` s = pd.Series([7,4,5,6], index=["a","b","c","d"])s[s == 8] Series([], dtype: int64) ```

# Integer Index

## Unique case

To get the integer index of a value in a Series, first convert the Series into an `Index`, and then use the `get_loc(~)` method:

``` s = pd.Series([7,4,5,6], index=["a","b","c","d"])pd.Index(s).get_loc(5) 2 ```

## Duplicate case

When there are duplicates, we get a NumPy array of booleans where `True` indicates the location of the value:

``` s = pd.Series([7,4,5,5], index=["a","b","c","d"])pd.Index(s).get_loc(5) array([False, False, True, True]) ```

If you want a list of integer indices instead of this boolean mask, use NumPy's `where(~)` method instead:

``` s = pd.Series([7,4,5,5], index=["a","b","c","d"])np.where(s==5) (array([2, 3]),) ```

## Non-existent case

If the value does not exist, we get a `KeyError`:

``` s = pd.Series([7,4,5,6], index=["a","b","c","d"])pd.Index(s).get_loc(9) KeyError: 9 ```
Edited by 0 others
thumb_up
thumb_down
Comment
Citation
Ask a question or leave a feedback...
thumb_up
2
thumb_down
0
chat_bubble_outline
0
settings
Enjoy our search
Hit / to insta-search docs and recipes!