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')
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.