Learning Pandas.Series(Part-4)(Why We Need Separate Indexers (loc,iloc ..)?)

Milankmr
Analytics Vidhya
Published in
4 min readMay 23, 2020
Photo by M. B. M. on Unsplash

In this part-4 of learning pandas , we will explore why we need indexers for indexing in Pandas.Series . If you jumped directly here , you may check the part-3 on the following link :

What we mean by Indexing and Indexers?

In Simple terms , 
Indexing means selecting all or some data from Series .
Indexers are the attributes that explicitly expose ways for indexing .

Why we need indexers ? It is very genuine question that why we need Indexers attributes separately when we may just use the Series Object and indexes associated with them as in below :

import pandas as pd
seriesObj = pd.Series([10,12,19,44,15,60],
index['20','30','10','20','40','50'],
name='series_name')
seriesObj['30'] --> 12
seriesObj[1] --> 12

As in the above example , we are able to select “12” using series indexes and implicit integer index ..

Question What is the issue with this ? Are you able to spot that ?Think before you scroll for the answer ..

Photo by Ben White on Unsplash

Hope you got the answer , let’s discuss the same together . To explore the problem we will create another series with numerical indexes .

import pandas as pd
#Series with explicit string indexes
series1 = pd.Series([10,12,19,44,15,60],
index=['20','30','10','20','40','50'],
name='series_name')
#Series with explicit integer indexes
series2 = pd.Series([10,12,19,44,15,60],
index=[1,2,3,4,5,6],
name='series_name')
series1[1] --> 12
series2[1] --> 10

As you might spot in the above example , we have two series :

series1 is with explicit string indexes and series2 is with explicit integer indexes .

By explicit we mean that we have provided the indexes while creating the series as you know we may create series without providing index which leads to auto generation of the same from 0 to len-1 .

Back to Answer , following is the issue :

In Case of explicit integer indexes , Operation such as series2[1] will use the explicit integer indexes and value corresponding to index value 1 is 10In Case of explicit non integer indexes , Operations such as series1[1] will use implicit integer indexes and value corresponding to index 1 is 12
Indexing issue with implicit and explicit indexing

This is about the issue while indexing without indexers , We also observe issues during slicing , there is one weird part in case of slicing without indexers ,Let’s discuss the same .

→Slicing :- Slicing is the mechanism where we can Extract Range based Subsets from different data structures like Series , Data Frame etc.

Similar to the indexing we may do the slicing in series with both explicit and implicit indexes , Let’s Check the same with following example .

#Slicing with non integer Explicit indeximport pandas as pd
#Series with string indexes
series1 = pd.Series([10,12,19,44,15,60],
index=['a','b','c','d','e','f'],
name='series_name1')
print(series1['a':'c'])
Slicing using non integers explicit index in pandas series

In the above example ,We used non integer explicit indexes for slicing , we get the output from index ‘a’ to ‘c’ both included . Now , Let’s use the implicit integer index for slicing for the same series.

Slicing using implicit indexes

Now , for the slicing using implicit indexes we got the output from 0:2 and here “2” is exclusive for output .

Note:- Another difference in slicing as compared to indexing explained below with example :

From the output of above example :

We got the same output for the series when we used integer indexes for the series for which explicit integer indexes were present . 

Thumb Rule if we use integers for the slicing they are always implicit indexes and does not depends on type of explicit indexes in series .

Above may seems confusing and yes it is , there are multiple rules to remember when not using “iloc” and “loc” .

I hope you would have got the answer of the question as title of the post , Let’s summarise the same :

To solve these potential issues and confusion with explicit integer indexes and slicing , Pandas provides separate location indexer attribute like "iloc" and "loc" 

In Short , iloc and loc indexers are the saviours from all the confusions and rules we had above😉

Photo by Victor Grabarczyk on Unsplash

Hope you learned something interesting and new from the article .Please share your thoughts and feedback on the same ,so that we may improve and help the fellow learners .

Happy Learning !!!

--

--

Milankmr
Analytics Vidhya

Working in MNC and trying to share my learning along the way to fellow learners :)