What is the purpose of the double square-brackets

Screen Link:

I was wondering what the difference and/or benefits of using a dataframe object over a series object are in this question. I’m specifically referring to the following sections of code.

Suggested Answer:

categories_mean = affordable_apps.groupby(
    ["affordability", "Category"]
).mean()[["Price"]]  <-----


if price < categories_mean.loc[(aff, cat)][0]: <----- Index needs to be provided

My code:

categories_mean = affordable_apps.groupby(
    ['affordability', 'Category']
).mean()['Price'] <-----


if price < categories_mean.loc[(aff, cat)]: <----- No index required

Clarification on this issue would be greatly appreciated.

I think my answer to a similar question here - What's happening here? .loc with indexing should provide some clarification.

In this particular scenario, there is no as such difference between your code and the one provided to us (in terms of output). But, double brackets are used when selecting multiple columns and the resulting output is a dataframe instead of a Series (which can be useful for certain situations which you might learn later).

In this case, it’s not really necessary to have double brackets as you discovered. I’d argue that your approach is simpler/better than the one in the Classroom for this particular exercise.

1 Like