FAQs on the exercise Calculating Aggregate Functions II
There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply () below.
If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.
Join the Discussion. Help a fellow learner on their journey.
Ask or answer a question about this exercise by clicking reply () below!
Agree with a comment or answer? Like () to up-vote the contribution!
to find the number of each category of tea they sell.
I don’t understant how this code works if they’re using the ‘id’ column, instead of the column ‘name’. Is the ‘id’ column a real column or just the index column? And if it’s a real DataFrame column, how does it works to count the different types of teas they have if the column contains just integers from 0 to the total number of teas? I mean, these integers never repeat, so how they count using this column?
Instead of the code they use, woudn’t be more clear to use:
In this example id is the name of a column. Pandas has a convenience option to use the . attribute syntax for column names in most circumstances (but not all).
In this case teas.groupby('category').id == teas.groupby('category')['id'] or more generally teas.id == teas['id']. Sometimes that syntax can be preferable or more readable, in other cases it may be more confusing.
Might be useful reading at this Q/A on the . dotted attribute syntax in pandas-
The count is probably the same but the column names would be the issue. In your first example you’d get something like-
category id
0 black 70
2 green 33
3 white 13
Note that the second column is titled ['id']. In the other route I think you’d wind up with two ['category'] columns or it just wouldn’t work, if you get the chance test it to see what happens (if this is just example data then try it on another dataframe you do have).