4

I am a bit confused of the iloc function of pandas, because I want to select a range of columns and the output is different than expected. The same will happen to row selection, so I wrote a little example:

template = pd.DataFrame(
    {'Headline': ['Subheading', '', 'Animal', 'Tiger', 'Bird', 'Lion'],
     'Headline2': ['', 'Weight', 2017, 'group1', 'group2', 'group3'],
     'Headline3': ['', '', 2018, 'group1', 'group2', 'group3']
     })

     Headline Headline2 Headline3
0  Subheading                    
1                Weight          
2      Animal      2017      2018
3       Tiger    group1    group1
4        Bird    group2    group2
5        Lion    group3    group3

I want to select line 1 to line 2 with print(template.loc[1:2]) the result is what I have expected:

  Headline Headline2 Headline3
1             Weight          
2   Animal      2017      2018

If I do this print(template.iloc[1:2]) I would think that I get the same result, but no:

  Headline Headline2 Headline3
1             Weight          

I am a bit confused, because I expected the same behavior for both functions, but the output of both functions differ if I select a range (FROM:TO).
It seems like using iloc needs to have the TO value +1 in order to have the same result as loc print(template.iloc[1:3]):

  Headline Headline2 Headline3
1             Weight          
2   Animal      2017      2018

Can someone put some light on it?

Erik Steiner
  • 581
  • 1
  • 5
  • 18
  • 1
    `iloc` is used for integer based indexing and end is not included. `loc` is used for label based indexing and end is included. – Krishna Aug 13 '18 at 08:17
  • look at third bullet point of [docs](https://pandas.pydata.org/pandas-docs/stable/indexing.html#different-choices-for-indexing) – Krishna Aug 13 '18 at 08:19

1 Answers1

5

As it mentioned in docs for loc:

Warning: Note that contrary to usual python slices, both the start and the stop are included

On the other hand, iloc do selects based on integer-location based indexing, so it doesn't include stop index.

Lev Zakharov
  • 2,409
  • 1
  • 10
  • 24