Introduction
This is an article with a focus for describing about how to access or to index Series. Before going on to the detail of accessing or indexing Series, it is very important for defining or declaring it first. In order to be able to define or to declare Series object variable, just read an article in ‘How to Use Pandas with Series‘. It will give information starting from the installation of Pandas library, defining or declaring Series by using Pandas library. After defining or declaring Series object variable, there are various function or operation available for processing it further.
How to Access or Index Series as Dictionary 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 :
-
First of all, as usual, execute Command Prompt. It is obvious since the example in this article for accessing or indexing Series object variable in Python is using command line. Moreover, it is using a device running using Microsoft Windows operating system. So, the appearance of the Command Prompt exist as follows :
Microsoft Windows [Version 10.0.22000.856] (c) Microsoft Corporation. All rights reserved. C:\Users\Personal>
-
Following after, just run Python command console by typing ‘python’ in the Command Prompt 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. >>>
If ‘python’ does not exist yet, try to install it first. Read an article in How to Install Python in Microsoft Windows or in How to Install Python in Microsoft Windows 11 as a reference for installing python.
-
Next, the important part before declaring or defining Series object variable which is importing the Pandas library 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
If the Pandas library does exist in the device just install it. Use the article in ‘How to Install Pandas‘ as an info for installing it.
-
After successfully importing Pandas library, just execute the following source code for declaring or defining DataFrame object variable and continue on accessing 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 >>> dictionary = {'name':'Kurt Walter','age':29,'sex':'M','height':189.5,'weight':63.2} >>> series_dictionary = pd.Series(dictionary) >>> series_dictionary.keys() Index(['name', 'age', 'sex', 'height', 'weight'], dtype='object') >>> series_dictionary.values array(['Kurt Walter', 29, 'M', 189.5, 63.2], dtype=object) >>>
So, in order to access an index of a Series which has the structure of dictionary collection data set, just use the key identifier. Accessing the Series as in the above example will have two methods. Obviously, those methods are for accessing elements of the Series. The first one is accessing the value of the key which is done by executing attribute ‘values’ as follows :
>>> series_dictionary.values array(['Kurt Walter', 29, 'M', 189.5, 63.2], dtype=object) >>>
On the other hand, there is another method exist for Series object variable using dictionary collection data set. It is method for accessing the index of the Series object variable. In this context, since dictionary use ‘key’ as its index, just execute the following method for accessing the index :
>>> series_dictionary.keys() Index(['name', 'age', 'sex', 'height', 'weight'], dtype='object') >>>
Next, another way for accessing Series is by using a specific key which exist in it. In this context, as an example, it is a key with the name of ‘name’ as follows :
>>> series_dictionary['name'] 'Kurt Walter' >>>