It’s not so much that "double brackets for
.iloc" is a thing in so much that it depends on the situation and what objects you’re working with. In fact, the above two code chunks are equivalent! To be clear: the “first bracket” is associated with
.iloc but the “second bracket” is indexing a Series. Therefore, I think you’re right…"double brackets for
.iloc" isn’t a thing.
If you look closely, you’ll see that the only difference between your code and that of DQ is that yours is split over two lines. If you were to skip the step of first defining
first_row and simply went straight to defining
top_japanese_employer in one line, it would be exactly the same as the DQ answer.
So why the “double brackets” here? Well, let’s take it step by step to see if we can logic our way through it:
sorted_rows is a DataFrame
- when we use
.loc on this DataFrame, we are returned a Series
- the returned Series has an index that comes from the column names of the original DataFrame
- therefore, when we use
first_row["company"] , we are returned the value under
company for the
first_row object (Series)
In fact, since
"company" is the first element of
first_row (or equivalently, the first element of
sorted_rows.iloc) we could actually do this:
top_japanese_employer = sorted_rows.iloc
Please tell me I didn’t just blow your mind?!
In the end, the “first bracket” returns a Series from a DataFrame and the “second bracket” returns a value from that Series based on its index. This is very similar to this:
list_of_lists = [[1,2,3], [4,5,6], [7,8,9]]
So my question to you is: what value will the code above print out for us and why?