Data Handling using Pandas - II
Aggregation, sorting and groupby
Use aggregate(), sort_values() and groupby().
aggregate() (or agg()) applies one or more functions at once: df.aggregate('max'), or df.aggregate(['max', 'count']). With axis=1, it works on rows.
sort_values() sorts a DataFrame: df.sort_values(by=['Maths'], ascending=False) puts the highest Maths marks first. Give a second column in the list to break ties: by=['Maths', 'Science'].
groupby() is one of the most powerful tools. It follows a split-apply-combine strategy: split the data into groups (for example, by Name), apply a function to each group, and combine the results into one table.
Looking at the groups made by g1 = df.groupby('Name'):
- g1.first(): the first row of each group;
- g1.size(): the number of rows in each group;
- g1.groups: which rows belong to each group;
- g1.get_group('Asha'): all the rows for Asha.
To summarise each group: df.groupby('UT')[['Maths', 'Science']].mean(), or .agg(['mean', 'var', 'std']). (The book prints the method as 'GROUP BY'; in Python you must type groupby.) Split, apply, combine.
Practise this lesson
Questions that get harder as you improve, a Tutor that helps without giving the answer away, and progress you can see. Free for every learner.
No account, no email address. Opens the real product.