How to calculate the values of a pandas DataFrame column depending on the results of a rolling function from another column
Question:   
A very simple example just for understanding.

**The goal is to calculate the values of a pandas DataFrame column depending
on the results of a rolling function from another column.**

I have the following DataFrame:

    
    
    import numpy as np
    import pandas as pd
    
    s = pd.Series([1,2,3,2,1,2,3,2,1])    
    df = pd.DataFrame({'DATA':s, 'POINTS':0})
    
    df
    

[![DataFrame
start](http://i.stack.imgur.com/8dZmD.png)](http://i.stack.imgur.com/8dZmD.png)

_Note: I don't even know how to format the Jupyter Notebook results in the
Stackoverflow edit window, so I copy and paste the image, I beg your pardon._

The **DATA** column shows the observed data; the **POINTS** column,
initialized to 0, is used to collect the output of a "rolling" function
applied to DATA column, as explained in the following.

Set a window = 4

    
    
    nwin = 4
    

Just for the example, **the "rolling" function calculate the max**.

Now let me use a drawing to explain what I need.

[![Algo
flow](http://i.stack.imgur.com/GflSG.png)](http://i.stack.imgur.com/GflSG.png)

For every iteration, the rolling function calculate the maximum of the data in
the window; then the POINT at the same index of the max DATA is incremented by
1.

The final result is:

[![DataFrame
end](http://i.stack.imgur.com/7P1Z5.png)](http://i.stack.imgur.com/7P1Z5.png)

Can you help me with the python code?

I really appreciate your help.  
Thank you in advance for your time,  
Gilberto

_P.S. Can you also suggest how to copy and paste Jupyter Notebook formatted
cell to Stackoverflow edit window? Thank you._


Answer: IIUC the explanation by @IanS (thanks again!), you can do

    
    
    In [75]: np.array([df.DATA.rolling(4).max().shift(-i) == df.DATA for i in range(4)]).T.sum(axis=1)
    Out[75]: array([0, 0, 3, 0, 0, 0, 3, 0, 0])
    

To update the column:

    
    
    In [78]: df = pd.DataFrame({'DATA':s, 'POINTS':0})
    
    In [79]: df.POINTS += np.array([df.DATA.rolling(4).max().shift(-i) == df.DATA for i in range(4)]).T.sum(axis=1)
    
    In [80]: df
    Out[80]: 
       DATA  POINTS
    0     1       0
    1     2       0
    2     3       3
    3     2       0
    4     1       0
    5     2       0
    6     3       3
    7     2       0
    8     1       0
    

