def regress(datayvarxvars) data[yvarx data[xvarsx['intercept' result sm ols(yxfit(return result params nowto run yearly linear regression of aapl on spx returnsi executein [ ]by_year apply(regress'aapl'['spx']out[ ]spx intercept - pivot tables and cross-tabulation pivot table is data summarization tool frequently found in spreadsheet programs and other data analysis software it aggregates table of data by one or more keysarranging the data in rectangle with some of the group keys along the rows and some along the columns pivot tables in python with pandas are made possible using the groupby facility described in this combined with reshape operations utilizing hierarchical indexing dataframe has pivot_table methodand additionally there is top-level pandas pivot_table function in addition to providing convenience interface to groupbypivot_table also can add partial totalsalso known as margins returning to the tipping data setsuppose wanted to compute table of group means (the default pivot_table aggregation typearranged by sex and smoker on the rowsin [ ]tips pivot_table(rows=['sex''smoker']out[ ]size tip tip_pct total_bill sex smoker female no yes male no yes this could have been easily produced using groupby nowsuppose we want to aggregate only tip_pct and sizeand additionally group by day 'll put smoker in the table columns and day in the rowsin [ ]tips pivot_table(['tip_pct''size']rows=['sex''day']cols='smoker'out[ ]pivot tables and cross-tabulation www it-ebooks info