How to Access or Index Series using iloc in Python

Posted on

Introduction

Another article which has similar content with the previous one. Actually, it extend or expand the explanation about how to access Series object variable. For more information, just access article ‘How to Access or Index Series as Array in Python’ for accessing or indexing the Series which has an array as its element. In order to access an element of Series object variable with array as its element, just use index. So, in this context, an index is a number represent the location for accessing the respective element. So, accessing the Series object variable of index zero (0) will retrieve its associated value. Furthermore, there is another alternative for accessing or retrieving the value using its associated index. It is by using ‘iloc’ feature from the Series object variable.

How to Access or Index Series using iloc in Python

So, this part will focus on how to access or to index Series using another alternative. Instead of stating directly the index, it will use a feature with the name of ‘iloc’. This is a feature where the main function is for locating element and retrieve it using the index of the element of Series object variable. But apparently, this article will have a connection with only. ‘Moreover, there is also another article ” for accessing or indexing the Series which is using array as part of the element.

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
    >>> array_variable = myarray.array("i",[1,2,3,4,5,6,7,8,9,10]) 
    >>> series_array = pd.Series(array_variable) 
    
  5. Finally, the last step which is focusing on how to access or index the element. So, using the ‘iloc’ feature exist in the Series object variable, just access it by specifying the index number. Below are several examples using ‘iloc’ to access and retrieve a single value from a single element by specifying the index number  :

    >>> series_array[0] 
    1
    >>> series_array[1] 
    2

    Moreover, there is another pattern for accessing more than one element which is retrieving more than one value. It is by specifying range from starting index to ending index. Below is one of the example :

    By not specifying the ending key, it will retrieve all of the values. Starts from the beginning 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_array[1:5]
    1 2
    2 3
    3 4
    4 5
    dtype: int64
    

    The above sample will access and retrieve value from index ‘0’ to index before the ending index which is index ‘4’. There is another variation of usage using this paatern as it exist below :

    >>> series_array[1:]
    1 2
    2 3
    3 4
    4 5
    5 6
    6 7
    7 8
    8 9
    9 10
    dtype: int64
    >>>
    

    Although the starting index exist followed by a colon, it does not have an ending index. Therefore, it will just retrieve all of the value from index ‘1’ until the last element

    Another variation of the iloc feature for accessing and retrieving value’s element is the skip value. By defining another parameter at the end of the ending index separated by another colon, it will define the skip value. In other words, it skip the retrieval value of a certain element. Below is an example to retrieve an odd and also an even number. It ranges from index ‘0’ to the last one by defining the skip value of ‘2’ :

    >>> series_array[0:10:2]
    0 1
    2 3
    4 5
    6 7
    8 9
    dtype: int64
    >>> series_array[1:10:2]
    1 2
    3 4
    5 6
    7 8
    9 10
    dtype: int64
    >>>
    

Leave a Reply