Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Grouping Data With groupby()
00:00 In this lesson, you’ll group data with groupby(), and you’ll learn about the one rule that trips up almost everyone who uses it. groupby() takes an iterable and a key function, and it yields pairs of a key and an iterator over the items in that group.
00:18 Here are four people with their ages.
00:28 Group them by age, and print each age with the names in its group. Alan and Catherine share a group, since they’re both thirty-four. Betsy and David each get a group of their own.
00:40 Without a key, groupby() groups identical items. But watch what happens with unsorted data. You get six groups, not three. One shows up twice, and two shows up three times.
00:53 Each time the value changes, groupby() starts a new group. Here’s why. groupby() walks through the data and keeps adding items to the current group until the key changes.
01:06 Then it starts a new group, even if it’s seen that key before. Unlike GROUP BY in SQL, it only groups neighbors. The fix is to sort the data by the same key first.
01:16 Then equal items sit next to each other, and you get one group per key. Sorting first is so common that it’s worth a small helper. sort_and_group() sorts the data with the same key it uses for grouping.
01:33 Now the same data gives you three groups, one per key. All the ones, all the twos, and the three end up together, because sorting put equal keys next to each other.
01:45 In this lesson, you grouped data with groupby(), saw that it only groups neighboring items, and wrote a helper that sorts before grouping. Next up, you’ll use that helper, together with batched(), to pick relay teams for a swim club.
Become a Member to join the conversation.