Label-based selection from a Series
In pandas, pd.Series.loc is used to perform selection by label instead of by position. This method is particularly useful when you consider the pd.Index of your pd.Series to contain lookup values, much like the key in a Python dictionary, rather than giving importance to the order or position of data in your pd.Series.
How to do it
Let’s create a pd.Series where we have a row index using integral labels that are also non-unique:
ser = pd.Series(["apple", "banana", "orange"], index=[0, 1, 1])
ser
0 apple
1 banana
1 orange
dtype: object
pd.Series.loc will select all rows where the index has a label of 1:
ser.loc[1]
1 banana
1 orange
dtype: object
Of course, you are not limited to integral labels in pandas. Let’s see what this looks like with a pd.Index composed of string values:
ser = pd.Series([2, 2, 4], index=["dog", "cat", "...