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 do this:</p>

<pre><code>(s.iloc[::2].values + s.iloc[1::2])/2
</code></pre>

<p>if you want you can also reset the index afterwards, so you have 0, 1 as the index, using:</p>

<pre><code>((s.iloc[::2].values + s.iloc[1::2])/2).reset_index(drop=True)
</code></pre>

python pandas group-by mean series