Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a new key in a list of dictionaries that is a sum of another key?

I have a dictionary like this:

[{'name': 'a', 'age': '20', 'value': '10'}, {'name': 'a', 'age': '30', 'value': '15'}, {'name': 'a', 'age': '40', 'value': '25'}, {'name': 'b', 'age': '20', 'value': '11'}, {'name': 'b', 'age': '30', 'value': '12'}, {'name': 'b', 'age': '40', 'value': '13'}...]

Now i want to add a new key with the sum of all values for the same name, like so:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

[{'name': 'a', 'age': '20', 'value': '10', 'sum': '50'}, {'name': 'a', 'age': '30', 'value': '15', 'sum': '50'}, {'name': 'a', 'age': '40', 'value': '25', 'sum': '50'}, {'name': 'b', 'age': '20', 'value': '11', 'sum': '36'}, {'name': 'b', 'age': '30', 'value': '12', 'sum': '36'}, {'name': 'b', 'age': '40', 'value': '13', 'sum': '36'}...]

I am already using Pandas for grouping and aggregating. Is this possible with Pandas as well or is there standard Python variant?

>Solution :

Yes, you can use Pandas to achieve this. Here’s an example code:

import pandas as pd

# create the dictionary
data = [{'name': 'a', 'age': '20', 'value': '10'},
        {'name': 'a', 'age': '30', 'value': '15'},
        {'name': 'a', 'age': '40', 'value': '25'},
        {'name': 'b', 'age': '20', 'value': '11'},
        {'name': 'b', 'age': '30', 'value': '12'},
        {'name': 'b', 'age': '40', 'value': '13'}]

# create a DataFrame from the dictionary
df = pd.DataFrame(data)

# convert the 'value' column to numeric values
df['value'] = pd.to_numeric(df['value'])

# create a new column 'sum' with the sum of 'value' for each 'name'
df['sum'] = df.groupby('name')['value'].transform('sum')

# convert the 'sum' column to string values
df['sum'] = df['sum'].astype(str)

# convert the DataFrame back to a list of dictionaries
result = df.to_dict(orient='records')

print(result)

This code creates a Pandas DataFrame from the dictionary, converts the ‘value’ column to numeric values, groups the DataFrame by ‘name’, calculates the sum of ‘value’ for each group, and creates a new column ‘sum’ with the sum of ‘value’ for each ‘name’. Finally, it converts the ‘sum’ column to string values and converts the DataFrame back to a list of dictionaries.

The output should be a list of dictionaries with the ‘sum’ column added for each ‘name’.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading