Loc versus [] in selecting a Series Row

Screen Link:

My Code:

age_proportions = wnba['Age'].value_counts(normalize=True).sort_index()*100

proportion_25 = age_proportions[25]/100
percentage_30 = age_proportions[30]
percentage_over_30 = age_proportions[30:].sum()
percentage_below_23 = age_proportions[:23].sum()

What I expected to happen:
I expect to get the summary of percentages for percentage_over_30 and percentage_below_23.

What actually happened:

I get no values

When I look at the solution it’s the same code but using .loc I do not understand why .loc needs to be specified here. The shorthand convention for choosing a slice of a series is Series[x:y]. It is even more confusing because the other lines of code use this exact syntax and are able to get the correct values.

1 Like

this will return first 23 rows regardless of whatever index it has.

while this, loc

age_proportions.loc[:23]

will select based on index label. So it will return rows that has index between 0 to 23. And in our case here index is starting from 21 so it will return first three rows 21, 22, and 23.

1 Like

Hey Dinshin,

If I understand correct, it is necessary here to specify the index in order for the the right slices of the series to be identified?

Yes, that is right index is necessary here.