BLACK FRIDAY EXTRA SAVINGS EVENT - EXTENDED
START FREE

Iloc vs "other" type of selection 370-2

the most confusing part of this exercise was trying to understand how everyone else got to their solution and seeing the answer to include one of the steps being

killed_manual_sum = killed.iloc[:,:3].sum(axis=1)

vs

killed_manual_sum = killed[['pedestrians_killed', 'cyclist_killed', 'motorist_killed']].sum(axis=1)

and everything is okay now but when I first saw iloc being used I had a minor freak out that I don’t intuitively know anything at all about selecting data.

comparing the two statements together and checking the .value_counts() assures that they’re definitely kind-of-maybe-sort-of-perhaps similiar but why did I just immediately think of this and everyone else, plus the question-builders thought of iloc instead? Will this be a problem for me in the future? Is this even remotely the same thing or would this have sent me flying off into the wrong direction?

Thanks.

p.s.

please let me know if i have labeled/tagged this incorrectly/inefficiently and how I can do better.

Hi @mctopherganesh,

I haven’t seen your dataframe, but:

  1. This piece of code:
killed[['pedestrians_killed', 'cyclist_killed', 'motorist_killed']]

(let’s for now ignore the part about sum(axis=1)), if assigned to a variable, will return you a new dataframe based of the killed dataframe and containing the 3 above-mentioned columns and all the rows from them.

  1. This piece of code:
killed.iloc[:,:3]

(again, here we don’t care about sum(axis=1)), assigned to a variable, will return you a new dataframe based on the killed dataframe, containing all the rows from it and the last 3 columns.

It means that if the last 3 columns of killed are pedestrians_killed, cyclist_killed, and motorist_killed, then in this case the 2 pieces of code above are absolutely identical. The one with iloc, though, looks more concise, while the one with the column names - more explanatory. So which one to use depends also on your particular task. It’s always good idea to keep the code clean and concise, but at the same time comprehensible and easy-to-follow.

As for iloc itself, which is used to select data by index, apart from the documentation, you can also look through this and this short articles.

Ah, and by the way, next time please put a link to the corresponding mission screen :slightly_smiling_face:

1 Like

@Elena_Kosourova

Thank you so much! This was really helpful. I updated my original post too.

1 Like

hi elena,

Thanks for that nice explanation, I was wondering about the fact if it was the first or rather the last 3 columns.
I guess its most likely a typo, but wouldn’t

killed.iloc[:,:3]
select the all elements of the first 3 columns rather then the last 3 columns?

For the selecting the last 3 columns, I would expect someting like the code below:
killed.iloc[:,-3:]

regards
Bender

1 Like

Hi Bender,

Absolutely correct, the first piece of your code selects the first 3 columns, the second one - the last 3.