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:
[{'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’.