python pandas - would like to vectorize this loop function and apply to a panel
<p>Would like to lean more on numpy/pandas vectorization than run the following loop procedure over a huge dataset. </p>

<pre><code>import pandas as pd
list1 = [[21,101],[22,110],[25,113],[24,112],[21,109],[26,108],[25,102],[26,106],[25,111],[22,110]]
dict1 = {}
dict1['close'] = pd.DataFrame(list1,index=pd.date_range('2000-1-1', periods=10, freq='D'), columns=list('AB'))
dict1['close_1'] = pd.DataFrame(dict1['close'] * (1-.05))
pan_so = pd.Panel(dict1).transpose(2,1,0)

win=5
def btwn(x,win):  # x is a dataframe
    for i in range(0,len(x.index)):
        s = 0
        if i &gt;= win:
            for w in range(1,win):
                if (x.ix[i,'close'] &lt;= x.ix[i-w,'close']) &amp; (x.ix[i,'close'] &gt;= x.ix[i-w,'close_1']):
                    s = s + 1
            x.ix[i,'cnt_s'] = s
    x = x.fillna(value=0,inplace=True)
</code></pre>

<p>run over 1 item of panel (a df):</p>

<pre><code>x = pan_so.ix['A',:,:]
btwn(x,win)
</code></pre>

<p>the output is:</p>

<pre><code>In[89]: x
Out[105]: 
                close  close_1  cnt_s
    2000-01-01   21.0    19.95    0.0
    2000-01-02   22.0    20.90    0.0
    2000-01-03   25.0    23.75    0.0
    2000-01-04   24.0    22.80    0.0
    2000-01-05   21.0    19.95    0.0
    2000-01-06   26.0    24.70    0.0
    2000-01-07   25.0    23.75    2.0
    2000-01-08   26.0    24.70    1.0
    2000-01-09   25.0    23.75    3.0
    2000-01-10   22.0    20.90    0.0
</code></pre>

<p>As you can imagine, this runs very slowly over a lot of data. Also cannot figure out how to apply the <code>btwn()</code> function to each item in the <code>pan_so</code> panel. Really appreciate any help.</p>