How to write long Pandas aggregations well?
Question: **TL;DR**

How do you write long aggregations involving many operations like `groupby()`,
`unstack()` or `apply()` well?

**Example**

Say you have a `DataFrame()` with `n_sales = 1000` ticket sales for `n_events
= 10` different events, like

    
    
    import pandas as pd
    import numpy as np
    
    sales = pd.DataFrame({
        'Event': np.random.choice(range(n_events), n_sales), 
        'Time': np.random.rand(n_sales)})
    

and you want to plot in how many events at least `n = [50, 100]` tickets were
sold over the evening: ![# of events over time where at least x/y tickets were
sold](http://i.stack.imgur.com/XpkpR.png)

Then I would do

    
    
    accumulation_of_sales = sales.groupby(['Time', 'Event']).size().unstack().fillna(0).cumsum()
    events_with_n_sales = accumulation_of_sales.apply(lambda x: x.value_counts(), axis=1).fillna(0)
    events_with_geq_n_sales = events_with_n_sales[events_with_n_sales.columns[::-1]].cumsum(axis=1)
    
    events_with_geq_n_sales[n].plot()
    

which seems hard to read to me and the lines are in principle too long (see
[PEP](https://www.python.org/dev/peps/pep-0008/#maximum-line-length)). So,

  * how are this specific and similar operations done best?
  * are there some tutorials/style guides/... for beginners? Maybe not particularly for Pandas, but similar languages?


Answer: One way to write multiline pandas queries is to use :

    
    
    accumulation_of_sales = sales.groupby(['Time', 'Event'])\
                                 .size()\
                                 .unstack()\
                                 .fillna(0)\
                                 .cumsum()
    

_I sometimes prefer to wrap these in parenthesis instead._

However, if you are doing several things here often there is a simpler way.
For example, whenever you see "groupby + unstack" you should think
"pivot_table":

    
    
    sales.pivot_table(columns='Event', index='Time', aggfunc=len, fill_value=0).cumsum()
    

_(Which is equivalent, more efficient and more readable.)_

