0

I was playing around with pandas and find the following feature is kind of strange.

For example, we have a data frame

df = pd.DataFrame({"egg":[47,50,89],"salt":[12.0,19.0,np.nan],"spam":[17,31,72]})
prinf(df,'\n')
print(df.iloc[0:2,0:3],'\n')
print(df.loc[0:2,"egg":"spam"],'\n')

enter image description here

So if I use .iloc, then for rows the row with index 2 is not selected. But if I use .loc, same command for selecting the rows, here the row with index 2 is selected.

Can someone explain why? This behavior is very unnatural.

KevinKim
  • 1,382
  • 3
  • 18
  • 34
  • http://stackoverflow.com/questions/31593201/pandas-iloc-vs-ix-vs-loc-explanation – RomanPerekhrest Jan 30 '17 at 20:34
  • 1
    This is basically because `.loc` is label based. It is designed for operations like `df.loc['Argentina':'France']` (where you would expect France to be included). For consistency, it behaves the same way with numbers. – ayhan Jan 30 '17 at 20:40

0 Answers0