Learning Pandas.Series(Part-6)(.iloc explored |.iloc vs .loc)

Milankmr
Analytics Vidhya
Published in
5 min readMay 25, 2020

In this part-6 of learning pandas , we will explore iloc indexers for indexing and slicing in Pandas.Series . If you jumped here directly, you may check Part-4 to know about why we need indexers and Part-5 for.loc indexer :

While learning .iloc ,we will also compare the same with .loc along the way , So , It would be better to go through the Part-5 of the series .

Photo by Markus Spiske on Unsplash

.iloc attribute refers to the implicit position indexes for indexing and slicing , it means it is based on position of data elements not the index labels .

import pandas as pdseries1 = pd.Series([10,12,19,44,15,60],
index=[3,4,2,1,5,6],
name='series_name1')
print("iloc works on position of element:",series1.iloc[4])
print("loc works on index label:",series1.loc[4])
Output:
iloc works on position of element: 15
loc works on index label: 12

series1.iloc[4] , 4 is the position of the element , position start from 0 and ends on len-1 same as that of array.

series1.loc[4] , 4 is the index label of the series.

iloc in series work on position of the element

If we try to access the position/index greater than len-1 , we will get IndexError: single positional indexer is out-of-bounds

import pandas as pd
series1 = pd.Series([10,12,19,44,15,60],
index=[3,4,2,1,5,6],
name='series_name1')
series1.iloc[6]Output:
IndexError: single positional indexer is out-of-bounds

In the example above there are total 6 elements and implicit position index exist from 0 to 5 . We tried to access index 6 which is not present , so We got Index Error (out-of-bounds).

Question :- In which scenario the output of loc and iloc will be same ? It is very straight ,Just think before scrolling for the Answer .

Photo by Nikita Kachanovsky on Unsplash

Answer:- Yes , you got that right !! If we don’t provide explicit index while creating a Series output with both .loc and .iloc for the series will be same because implicit indexes are always present from 0 to len-1 .

import pandas as pd
series1 = pd.Series([10,12,19,44,15,60],
name='series_name1')
print("Output with iloc:",series1.iloc[3])
print("Output with loc:",series1.loc[3])
Output with iloc: 44
Output with loc: 44
loc and iloc generates same output for a series if explicit indexes are not defined

Fancy Indexing with .iloc : In case you want to select values for multiple position indexes in one go which are not consecutive , you may use fancy indexing in which you pass in the list of position indexes you want to get data

import pandas as pd
series1 = pd.Series([10,12,19,44,15,60],
index=[3,4,2,1,5,6],
name='series_name1')
series1.iloc[[1,4,2]]1 12
4 15
2 19
Name: series_name1, dtype: int64

Note:- Keep in mind that with iloc 1,4 and 2 are position indexes not label indexes

Boolean array as indexer for series with iloc:- We may use the boolean array as an indexer , the indexes will be choosen for which value is true in the array .

import pandas as pd
series1 = pd.Series([10,12,19,44,15,60],
index=[3,4,2,1,5,6],
name='series_name1')
print("======Output With iloc=======")
print(series1.iloc[[True,False,True,False,True,False]])
print("======Output With loc========")
print(series1.loc[[True,False,True,False,True,False]])
======Output With iloc=======
3 10
2 19
5 15

Name: series_name1, dtype: int64
======Output With loc========
3 10
2 19
5 15

Name: series_name1, dtype: int64

So , It is same as that of .loc attribute for boolean array as indexer and you guessed it right :- The output of both .loc and .iloc with boolean array will be same .

This was the head start for indexing with .iloc attribute in series , let’s start to understand slicing using .iloc attribute

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

Slicing with .iloc attribute refers to implicit position indexes in the series and not the labeled indexes :

Format of slice → [start]:[end][:step] → start , end and step are integers and square brackets represents the optional values .

There are 2 main differences when using .iloc as compared to .loc :

  1. .iloc uses implicit position indexes where as .loc uses explicit labeled indexes .
  2. Another main difference is in .iloc [end] is exclusive whereas [end] is inclusive in case of .loc as we discussed in last Part-5

Let’s check the same with following example:

import pandas as pd
series1 = pd.Series([10,12,19,44,15,60],
index=['0','1','2','3','4','5'],
name='series_name1')
print("=== .iloc refers to implicit position indexes and End is Exclusive while slicing with iloc ===")
print(series1.iloc[0:2])
print("\n")
print("=== .loc refers to explicit labeled indexes and End is Inclusive while slicing with iloc ===")
print(series1.loc['0':'2'])

Output:- So , as in the code above , for .iloc we move from position index 0 to 1 because [end] →2 is exclusive for .iloc and for .loc we move from ‘0’ to ‘2’ becasue [end] →’2' is inclusive for .loc .

=== .iloc refers to implicit position indexes and End is Exclusive while slicing with iloc === 
0 10
1 12
Name: series_name1, dtype: int64
=== .loc refers to explicit labeled indexes and End is Inclusive while slicing with iloc ===
0 10
1 12
2 19
Name: series_name1, dtype: int64
[end] is inclusive for .loc and exclusive for .iloc

Optional start/end in slicing : As seen in the format , [start] and [end] are optional ,

If we don’t provide any value for start , it start with the first index and

if we don’t provide end , it will consider last index as end .

import pandas as pd
series1 = pd.Series([10,12,19,44,15,60],
index=['a','c','d','e','b','f'],
name='series_name1')
print(series1.iloc[:4]) #will stop at index 3 coz 4 is exclusive
print(series1.iloc[3:]) #will start from index 3 coz 3 is inclusive
Output:
a 10
c 12
d 19
e 44

Name: series_name1, dtype: int64
e 44
b 15
f 60

Name: series_name1, dtype: int64
Default start and end indexing while slicing with .iloc

So , this was about head start with .iloc indexer with series in pandas , In the next Part of the series we will explore about more attributes for Series and will cover some important methods of series !!

Hope you enjoyed the article and learnt something new today . Keep exploring and Keep Learning !!!

--

--

Milankmr
Analytics Vidhya

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