How do I calculate the percent difference between the 1st and 6th key in a list of dictionaries?
<p>I have a pandas df column with a list of dictionaries for each each company name. Like below:</p>

<pre><code>company  |  growth_scores
comp xyz | [{u'score': u'198', u'recorded_at': u'2016-09},{u'score': u'190', u'recorded_at': u'2016-08} 
</code></pre>

<p>I understand how to extract the keys and I'm familiar with the pd.apply method but I can't seem to piece together anything that will go row-by-row and perform the calculation. Ultimately, I need to perform a calculation and store the result in a new column, for each company. </p>

<p>The output should look like this:</p>

<pre><code>company  |  growth_score_diff
comp xyz |  10%
</code></pre>

<p>Would love some guidance here! </p>

<p>Say you have the following DataFrame:</p>

<pre><code>df = pd.DataFrame.from_dict({'company': 'Pandology', 'metrics': [[{'score': 10}, {'score': 20}, {'score': 35}]]})
</code></pre>

<p>which looks like this:</p>

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

<p>To compute a total score, you can <code>map</code> the <code>metrics</code> column to a new column called <code>score_total</code>. To perform the actual calculation, you define a function <code>calculate_score</code> which takes a row of <code>metrics</code> data as input and outputs a total score value. (<em>In this case it's just a trivial sum calculation)</em></p>

<pre><code>def calculate_score(metrics):
    total_score = 0
    for metric in metrics:
        total_score += metric['score']
    return total_score                

df['score_total'] = df['metrics'].map(calculate_score)
</code></pre>

<p>Now you have a new column containing the result:</p>

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

python list pandas dictionary