Data Handling using Pandas - II
Maximum, minimum, sum and count
Use max(), min(), sum() and count() on the unit test data.
Descriptive statistics summarise data with a few numbers: maximum, minimum, count, sum, mean, median, mode, quartiles and variance. Pandas has a function for each.
The book's case study is a DataFrame of marks: student Name, the unit test (UT) number, and marks in several subjects.
- max() and min() give the largest and smallest value of each column. Add numeric_only=True to leave out text columns.
- axis=1 works along each row instead: for example, a student's best mark in one test.
- count() counts the non-missing values; with axis=1, it counts them in each row.
- sum() adds the values. On a text column, sum() joins the strings together, so pick the number columns: df['Science'].sum().
Often you want a summary for only some rows. Filter first, then apply the function: df[df.UT == 3] gives only the third unit test; df[df.Name == 'Asha'] gives only Asha's rows. Then df[df.UT == 3]['Maths'].max() gives the top Maths mark in UT 3.
Filter, choose the columns, then summarise.
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.