Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas iloc returns different range than loc [duplicate]

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?

like image 636
Erik Steiner Avatar asked Oct 23 '25 08:10

Erik Steiner


1 Answers

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.

like image 172
Lev Zakharov Avatar answered Oct 25 '25 21:10

Lev Zakharov