How to Access or Index Series as Array in Python

Posted on

Introduction

This is another article which is also focusing on Series object variable in Python aside from several previous article. In order to use Series, just read an article in ‘How to Use Pandas with Series‘. After getting Pandas library in the device, the process can continue on. In this context, the process for accessing or indexing Series in Python programming language. Actually, another similar article has already exist in the previous article. That article is in How to Access or Index Series as Dictionary in Python which is showing to access or to index Series object variable using dictionary collection data sets as its element. On the other hand, this article will use a normal and simple array as the element of the Series object variable.

How to Access or Index Series as Array in Python

Before going on further, just make sure that the requirement for defining or declaring Series object variable in Python programming language exist. Just read as a reference the article exist in ‘How to Use Pandas with Series‘. After ensuring all of the requirements, just execute the following steps in sequence :

  1. Run and execute Command Prompt as the example will use a device running using Microsoft Windows operating system. Furthermore, the example’s execution will use Python command console in the command line interface. Below is the Command Prompt appearance :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    C:\Users\Personal>
    
  2. After ensuring python exist in the operating system, just execute python by typing ‘python’. It exist below as in the Command Prompt :

    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.
    >>> 
    

    On the other hand, if ‘python’ does not exist in the device, just read How to Install Python in Microsoft Windows or How to Install Python in Microsoft Windows 11. It is useful for helping the python installation as reference.

  3. In the python command console, just execute the following command before declaring or defining Series object variable. It is a step for importing the Pandas library :

    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
    

    As it exist in the above execution, the Pandas library import process is a success. If it ends in a failure, it means the Pandas library does not exist yet. Just read ‘How to Install Pandas‘ for installing Pandas library.

  4. So, since Pandas library exist after the import process, the next process is just executing source code for declaring or defining DataFrame object variable. Continuing on the declaration and definition of DataFrame object frame, just access all of the elements inside in the Series 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
    >>> import array as myarray
    >>> array_var = myarray.array('i',[1,2,3,4,5,6,7,8,9,10])
    >>> series_array = pd.Series(array_var)
    

    Since the declaration of the Series using an array o, in order to access an index of a Series which has an array as the element, just use the array index. So, the access can only be possible using the index number in order to retrieve elements of the Series. Below is an example for accessing single element of the Series exist in the first index :

    >>> series_array[0]
    1
    >>>
    

    Move on further, below is an example for accessing multiple elements of the Series using a range of the beginning index and before the last index :

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

    Moreover, there is another different format for accessing the Series object variable by adding another parameter. Just add another parameter in the end to define the steps. The steps is very useful for retrieving elements by skipping a number of elements in between. Below is an example of it to retrieve just the odd number using step value of ‘2’ :

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

    Another one below is an example using step value of ‘2’ to retrieve even numbers :

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

Leave a Reply