Group by basics
True mastery of the pandas group by mechanisms is a powerful skill for any data analyst. With pandas, you can easily summarize data, find patterns within different groups, and compare groups to one another. The number of algorithms you can apply alongside a group by are endless in theory, giving you as an analyst tons of flexibility to explore your data.
In this first recipe, we are going to start with a very simple summation against different groups in an intentionally small dataset. While this example is overly simplistic, a solid theoretical understanding of how group by works is important as you look toward real-world applications.
How to do it
To get familiarized with how group by works in code, let’s create some sample data that matches our starting point in Figures 8.1 and 8.2:
df = pd.DataFrame([
["group_a", 0],
["group_a", 2],
["group_b", 1],
["group_b", 3],
["group_b"...