in [ ]demeaned out[ ] joe - steve - - - wes - nan nan - - jim - - travis - - - - you can check that demeaned now has zero group meansin [ ]demeaned groupby(keymean(out[ ] one - two - as you'll see in the next sectiongroup demeaning can be achieved using apply also applygeneral split-apply-combine like aggregatetransform is more specialized function having rigid requirementsthe passed function must either produce scalar value to be broadcasted (like np meanor transformed array of the same size the most general purpose groupby method is applywhich is the subject of the rest of this section as in figure - apply splits the object being manipulated into piecesinvokes the passed function on each piecethen attempts to concatenate the pieces together returning to the tipping data set abovesuppose you wanted to select the top five tip_pct values by group firstit' straightforward to write function that selects the rows with the largest values in particular columnin [ ]def top(dfn= column='tip_pct')return df sort_index(by=column)[- :in [ ]top(tipsn= out[ ]total_bill tip sex smoker female yes male yes male no female yes female yes male yes day sat sun sat sat sun sun time size tip_pct dinner dinner dinner dinner dinner dinner nowif we group by smokersayand call apply with this functionwe get the followingin [ ]tips groupby('smoker'apply(topout[ ]total_bill tip sex smoker smoker day data aggregation and group operations www it-ebooks info time size tip_pct