Built with Tutify.io

    AIStudyLabs

    Pandas Series loc[] Method

    Pandas Series loc[] Method

    Pandas Series.loc[] function is used to access a group of rows and columns by labels or a boolean array in the given Series object. We can select some values from the given Pandas Series object based on the labels using loc[] function. In this article, I will explain Series.loc[] to select Series object values by index labels.

    Key Points –

    <a id="h-quick-examples-of-series-loc-function"></a>

    Quick Examples of Series loc[] Function

    If you're in a hurry, here are some quick examples of the Pandas Series loc[] method.

    
    # Quick examples of series loc[] function
    
    # Examples 1: Use Series.loc[] function 
    # To selected labels
    ser2 = ser.loc[['Pandas', 'Spark', 'Python']]
    
    # Examples 2: Select Rows Between two Index Labels
    # Includes both Spark and Python rows
    ser2 = ser.loc['Spark':'Python']
    
    # Examples 3: Select Alternate rows by indeces
    ser2 = ser.loc['Spark':'Pandas':2]
    
    # Examples 4: Use loc[] and lambda
    ser2 = ser.loc[lambda x : x == 28000]
    
    # Examples 5: Use loc[] property & OR condition
    ser2 = ser.loc[lambda x : (x  28000)]
    

    <a id="h-usage-of-pandas-series-loc-method"></a>

    Usage of Pandas Series loc[] Method

    The loc[] method in a Pandas Series is primarily used for label-based indexing, allowing you to access, filter, or modify data by the label of the index. It is useful when you want to select rows or values based on their labels.

    To run some examples of the pandas series loc[] method, let's generate a Pandas Series.

    
    import pandas as pd
      
    # Create the Series
    ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
      
    # Create the Index
    index = ['Java','Spark','PySpark','Pandas','python NumPy','Python',"Oracle"]
      
    # Set the index
    ser.index = index
    print(ser)
    

    Yields below output.

    
    # Output:
    Java            20000
    Spark           25000
    PySpark         23000
    Pandas          28000
    python NumPy    55000
    Python          23000
    Oracle          28000
    dtype: int64
    

    <a id="h-use-series-loc-to-select-values-from-series"></a>

    Use Series.loc[] to Select Values from Series

    You can use the Pandas Series.loc[] function to get or select the single value by Index or multiple values by index by specifying the index labels (in a list) from the Series object.

    
    # Use Series.loc[] function to selected labels
    ser2 = ser.loc[['Pandas', 'Spark', 'Python']]
    print(ser2)
    

    Yields below output.

    
    # Output:
    Pandas    28000
    Spark     25000
    Python    23000
    dtype: int64
    

    <a id="h-select-rows-between-two-index-labels"></a>

    Select Rows Between Two Index Labels

    loc[] also supports selecting rows or values between two index labels by range in pandas Series. For instance, all items between starting and ending index. The below example selects rows between Spark and Python.

    
    # Select Rows Between two Index Labels
    # Includes both Spark and Python rows
    ser2 = ser.loc['Spark':'Python']
    print(ser2)
    

    Yields below output.

    
    # Output:
    Spark           25000
    PySpark         23000
    Pandas          28000
    python NumPy    55000
    Python          23000
    dtype: int64
    

    <a id="h-select-alternate-rows-by-indices"></a>

    Select Alternate Rows By Indices

    Similarly, by using ranges you can also select every alternate row from Series. In order to do so, you need to provide the third value as a step.

    
    # Select Alternate rows by indeces
    ser2 = ser.loc['Spark':'Pandas':2]
    print(ser2)
    

    Yields below output.

    
    # Output:
    Spark     25000
    Pandas    28000
    dtype: int64
    

    <a id="h-use-series-loc-and-lambda"></a>

    Use Series.loc[] and Lambda

    You can also use Pandas Series.loc[] along with lambda function. The following example returns values from a series where values are equal to 28000.

    
    # Use loc[] and lambda
    ser2 = ser.loc[lambda x : x == 28000]
    print(ser2)
    

    Yields below output.

    
    # Output:
    Pandas    28000
    Oracle    28000
    dtype: int64
    

    <a id="h-use-series-loc-amp-or-condition"></a>

    Use Series.loc[] & OR Condition

    You can also apply a “OR” condition with the “loc[]” property. The following example returns the values that are less than 25000 or values greater than 28000. For examples.

    
    # Use loc[] property & OR condition
    ser2 = ser.loc[lambda x : (x  28000)]
    print(ser2))
    

    Yields below output.

    
    # Output:
    Java            20000
    PySpark         23000
    python NumPy    55000
    Python          23000
    dtype: int64
    

    <a id="h-complete-examples-of-pandas-series-loc"></a>

    Complete Examples of Pandas Series loc

    
    import pandas as pd
      
    # Create the Series
    ser = pd.Series([20000,25000,23000,28000,55000,23000,28000])
      
    # Create the Index
    index = ['Java','Spark','PySpark','Pandas','python NumPy','Python',"Oracle"]
      
    # Set the index
    ser.index = index
    print(ser)
    
    # Use Series.loc[] function to selected labels
    ser2 = ser.loc[['Pandas', 'Spark', 'Python']]
    print(ser2)
    
    # Select Rows Between two Index Labels
    # Includes both Spark and Python rows
    ser2 = ser.loc['Spark':'Python']
    print(ser2)
    
    # Select Alternate rows by indeces
    ser2 = ser.loc['Spark':'Pandas':2]
    print(ser2)
    
    # Use loc[] and lambda
    ser2 = ser.loc[lambda x : x == 28000]
    print(ser2)
    
    # Use loc[] property & OR condition
    ser2 = ser.loc[lambda x : (x  28000)]
    print(ser2)
    

    <a id="h-faq-on-pandas-series-loc-method"></a>

    FAQ on Pandas Series loc[] Method

    The loc[] function in Pandas Series facilitates label-based indexing, allowing users to access specific elements or slices of data based on their index labels.

    The loc[] function supports inputs such as single labels, lists of labels, or label slices, providing flexibility in fetching desired data from a Pandas Series.

    By enabling precise selection of rows or columns based on their index labels, the loc[] function streamlines data manipulation tasks in Pandas Series, enhancing the efficiency of data analysis workflows.

    The loc[] function plays a crucial role in retrieving specific data points or subsets from a Pandas Series based on label-based indexing, ensuring accurate and efficient data retrieval.

    Suppose we have a Pandas Series named data with index labels 'A', 'B', 'C', 'D', and 'E'. Using data.loc['B'] would retrieve the value corresponding to the label 'B' in the Series.

    <a id="h-conclusion"></a>

    Conclusion

    In this article, you have learned about pandas Series loc property. Series.loc[] is label-based to select some values from the given Series object in pandas with examples.

    Happy Learning !!

    <a id="h-related-articles"></a>

    Related Articles

    <a id="h-references"></a>

    References