Summing the values of one element of a dictionary based upon the values of another element
<p>Using Python, I have a list of two-element dictionaries which I would like to sum all the values of one element based upon the values of another element. ie.</p>

<pre><code>[{'elev': 0.0, 'area': 3.52355755017894}, {'elev': 0.0, 'area': 3.5235575501288667}]
</code></pre>

<p>This is the format (although there are much more entries than this), and for each different <code>elev</code> I would like to have the sum of all the <code>area</code> values that correspond to it.  For the <code>elev</code> value of <code>0.0</code> I would like the sum of all values, same for <code>elev</code> of <code>0.1</code> etc</p>

<p>this is very easily achieved using pandas. Sample code:</p>

<pre><code>import pandas as pd
df = pd.DataFrame([{'elev': 0.0, 'area': 3.52355755017894}, {'elev': 0.0, 'area': 3.5235575501288667}])
</code></pre>

<p>which gives the following dataframe:</p>

<pre><code>       area  elev
0  3.523558   0.0
1  3.523558   0.0
</code></pre>

<p>Then group by the elev columns and sum the area's:</p>

<pre><code>desired_output = df.groupby('elev').sum()
</code></pre>

<p>which gives:</p>

<pre><code>              area
elev          
0.0   7.047115
</code></pre>

<p>If you want you can then output this dataframe back to a dictionary in a useful format using:</p>

<pre><code>desired_output.to_dict('index')
</code></pre>

<p>which returns</p>

<pre><code>{0.0: {'area': 7.0471151003078063}}
</code></pre>

python dictionary