apply function on groups of k elements of a pandas Series
<p>I have a pandas Series:</p>

<pre><code>0     1
1     5
2    20
3    -1
</code></pre>

<p>Lets say I want to apply <code>mean()</code> on every two elements, so I get something like this:</p>

<pre><code>0    3.0
1    9.5
</code></pre>

<p>Is there an elegant way to do this?</p>

<p>You can use <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.groupby.html" rel="nofollow"><code>groupby</code></a> by <code>index</code> divide by <code>k=2</code>:</p>

<pre><code>k = 2
print (s.index // k)
Int64Index([0, 0, 1, 1], dtype='int64')

print (s.groupby([s.index // k]).mean())
   name
0   3.0
1   9.5
</code></pre>

python pandas group-by mean series