Lesson 2 ❮ Lesson List ❮ Top Page
❯ 2.2 Grouping Object
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 6m 31s
☷ Interactive readings 5m
We will create a pandas DataFrame groupby object using the pandas DataFrame groupby method, passing it a column or list of columns for the grouping. In the movie dataset, we will try to do grouping by 'director_name'.
We can use get_group to get all the rows for a particular group. Say, we want to get the data about the director 'Steven Spielberg'. The get_group method will show all the rows containing that director.
To show the number of rows for each director, we can use the method size.
We can also use first and last method of the groupby object to create DataFrames with the first and last occurrence of each group.
GroupBy object is not limited to one column name. We can put in a list and select a particular group for each element in that list.