Please bear with me. I am new to pandas and using series within it.
import pandas as pd
s = pd.Series(["koala", "dog", "chameleon"])
'dog' in s.values
- this allows test if it is present or otherwise.
s.isin(['dog']).any()
- this works true. But there isn't any way to get index or use find.
How do I find index/location of "dog"?
Second, if I have duplicate entries (of dog, for e.g.):
s = pd.Series(["koala", "dog", "chameleon","dog"])
How would I be able to find first or last occurrence?
I am on python 3X (OS X, M1):
Python 3.9.8 (v3.9.8:bb3fdcfe95, Nov 5 2021, 16:40:46) [Clang 13.0.0 (clang-1300.0.29.3)] on darwin
You can use [].index
to get the index of a value in a series
.
s = pd.Series(["koala", "dog", "chameleon"])
s[s == 'dog'].index
Similarly to get the first and last occurrence using min()
and max()
:
s = pd.Series(["koala", "dog", "chameleon","dog"])
d_first, d_last = s[s == 'dog'].index.min(), s[s == 'dog'].index.max()