How to Access or Index Series using loc in Python

Posted on

Introduction

This article exist as a variation of another previous article. It is just access article of ‘How to Access or Index Series as Dictionary in Python‘. That article is describing on how to access or to index the Series using dictionary as part of the element. Actually, in that article, there is a normal and simple way for accessing or indexing the Series which has Series as its element. It is by mentioning the key which is actually serve as the index of the element. So, accessing or indexing the Series using appropriate key will retrieve the associated value respectively. On the other hand, this article will provide information about how to be able to do it using another alternative. It is by using the ‘loc’ attribute which is available as part of the Series object variable.

How to Access or Index Series using loc attribute in Python

In order to be able to demonstrate how to use ‘loc’ in order to access or to index an element from the Series object attribute, below are the steps for achieving it :

  1. First of all, Command Prompt execution since it uses Microsoft Windows operating system running device. Furthermore, it will demonstrate the steps in a command line interface. Below is the appearance of the Command Prompt :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>
    
  2. In the second step, just type ‘python’ in the Command Prompt for getting in to the Python command console as follows :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    

    Read ‘How to Install Python in Microsoft Windows‘ or ‘How to Install Python in Microsoft Windows 11‘ for information on installing python if it does not exist in the device yet.

  3. In the third step after the previous one, just import Pandas library for the sake of defining or declaring the Series object variable. It exist as follows :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pandas as pd
    >>> series_dictionary = pd.Series(dictionary) 
    

    If the Pandas library import ends in a failure, install it fist. Access ‘How to Install Pandas‘ or ‘How to Use Pandas‘ for more information on installing or using it.

  4. After that, the main steps will follow by defining and declaring the necessary Series object variable with dictionary as its element below :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import pandas as pd
    >>> dictionary = {"name":"Michael","age":27,"height:",179.4,"weight":69.3}
    >>> series_dictionary = pd.Series(dictionary) 
    
  5. Following after, it will just the sequence for accessing or indexing the Series. The first example is just accessing a single element using its associated key. With an alternative way by using ‘loc’, the following is the actual execution of the ‘loc’ for accessing a single ‘key’ to retrieve its value :

    >>> series_dictionary.loc['name']
    'Michael'
    >>> >>> series_dictionary.loc['age']
    27
    >>>
    

    Another variation of the ‘loc’ feature, it is by using the starting key and the ending key. It exist by the pattern of loc[‘starting_key’:’ending_key’]. Below is an example where the starting key is clearly mentioned but without an ending key as follows :

    >>> series_dictionary.loc['name':]
    name Michael
    age 27
    height 179.4
    weight 69.3
    dtype: object
    >>>
    

    By not specifying the ending key, it will retrieve all of the value from the starting key to the ending key in their order respectively. Below is also another example for retrieving several values using several keys but with a different starting key :

    >>> series_dictionary.loc['age':]
    age 27
    height 179.4
    weight 69.3
    dtype: object
    >>>
    

    Since it starts from the ‘age’ key, it will not print or retrieve the value for the ‘name’ key. The reason is because the starting key is not ‘name’ but ‘age’. So, it will print all of the value exist in all of keys after ‘age’. More example is another variation of the pattern using ‘loc’. It is specifying steps or interval for retrieving the element or the value. Below is the access or the index for retrieving the value with the step of ‘2’ :

    >>> series_dictionary.loc['name':'weight':2]
    name Michael
    height 179.4
    dtype: object
    >>>
    

    Since there is a step value of ‘2’, it will skip the retrieving the value of the next key. Instead of printing ‘age’, it directly print ‘height’ after ‘name’ as in the output above.

Leave a Reply