Resample 2d coordinates with values in pandas
<p>I have a data frame in pandas containing x coordinates, y coordinates, and values.
I would like to "downsample" my coordinates and add the values appropriately.</p>

<pre><code>df = pd.DataFrame({'x': [1,2,3,4,1,2,3,4,1,2,3,4,1,2,3,4],
                   'y': [1,1,1,1, 2,2,2,2, 3,3,3,3, 4,4,4,4], 
                   'v': [1,1,1,1,1,1,1,1,1,1,1,2,1,2,1,1] })
</code></pre>

<p>Visualizing this data frame with a scatter plot looks like:</p>

<p><a href="http://i.stack.imgur.com/bvAZe.png" rel="nofollow"><img src="http://i.stack.imgur.com/bvAZe.png" alt="enter image description here"></a></p>

<p>and I want to resample my data to something like this:
<a href="http://i.stack.imgur.com/Tb9BC.png" rel="nofollow"><img src="http://i.stack.imgur.com/Tb9BC.png" alt="enter image description here"></a></p>

<p>The v values should simply be added together.
I also would like to choose the factor of the sampling.</p>

<p>I first looked into df.resample but this only applies to time series data. I assume this can be achieved with groupby() somehow but it seems I just can't figure it out. Any hints are highly appreciated.</p>

<p>Try this</p>

<pre><code>g = df.groupby([df.x.sub(1) // 2 * 2 + 1.5,
                df.y.sub(1) // 2 * 2 + 1.5])
g.v.sum().reset_index()
</code></pre>

<p><a href="http://i.stack.imgur.com/NDeOt.png" rel="nofollow"><img src="http://i.stack.imgur.com/NDeOt.png" alt="enter image description here"></a></p>

python python-3.x pandas