How is it possible to retrieve the labe of a particular value in a pandas Series object:
For example:
labels = ['a', 'b', 'c', 'd', 'e']
s = Series (arange(5) * 4 , labels)
Which produces the Series:
a 0
b 4
c 8
d 12
e 16
dtype: int64
How is it possible to get the label of value '12'? Thanks
Accessing Element from Series with Position In order to access the series element refers to the index number. Use the index operator [ ] to access an element in a series. The index must be an integer. In order to access multiple elements from a series, we use Slice operation.
Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.
iloc attribute enables purely integer-location based indexing for selection by position over the given Series object. Example #1: Use Series. iloc attribute to perform indexing over the given Series object.
loc attribute is used to access a group of rows and columns by label(s) or a boolean array in the given Series object. Example #1: Use Series. loc attribute to select some values from the given Series object based on the labels.
You can get the subseries by:
In [90]: s[s==12]
Out[90]:
d 12
dtype: int64
Moreover, you can get those labels by
In [91]: s[s==12].index
Out[91]: Index([d], dtype=object)
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