Hacker News new | past | comments | ask | show | jobs | submit login

Note to author:

> We can select one or multiple rows using their numbers (inclusive of both bounding row numbers):

> df[1:3]

That will slice beginning from the row with integer location 1 up to 3, exclusive of the last element. So, just two rows, not three as shown.




Thanks for the heads up! Indeed it's df.loc[1:3] that would return three rows, not straight-up df[1:3] which indeed returns two rows.

Edit: Corrected in the post. Thanks again!


Genuine question, did you run the original before publishing it on the website?


But why design it that way? Seems to be a sure way to confuse new user.


I'd say that's because df[start:stop] mimics python's builtin list slicing, which includes start but excludes end, so this dictates the behaviour of indexing without .loc. By contrast, df.loc[start:stop] is a label-based indexer, and labels can be anything (integers, strings, datetimes, categories, etc.), so it doesn't always make sense to exclude the right endpoint of the interval.


(^^)b




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: