Home > Back-end >  Sum all values in a json with the same key?
Sum all values in a json with the same key?

Time:05-18

Here is a sample of the json:

{'address': 'erd1veq4j5x7nd4pxskk0shy2fy667gkqe2u48scpadanakntqjg5zgqf4ar7v', 'balance': '1'}
{'address': 'erd1zalw9d49xrxtw2hktfd59slln223d59akmrh45e8n4p3vpvkze4q2d9790', 'balance': '1'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '1'}
{'address': 'erd1x08ejc36w7vpqcwgcu3wxcd9akax3y6kwfmx5xxjnmtrwgrw90ssw93mzu', 'balance': '1'}
{'address': 'erd1mg3u5fm0d4rquxpr7hxt096507ze4j4xpngfuweynewc9cphgy8q4y972q', 'balance': '1'}
{'address': 'erd10594czlrsddna633r4vl33rdq93ejxc9p3g5gne9tr38msz2lg7s73gzf0', 'balance': '1'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '10'}
{'address': 'erd1ztcpuncemxcpes6t58cs28acre0zarjh6zj32a4e9vyhfp3g5agq0qtvqv', 'balance': '1'}
{'address': 'erd1ztcpuncemxcpes6t58cs28acre0zarjh6zj32a4e9vyhfp3g5agq0qtvqv', 'balance': '1'}
{'address': 'erd1tjlkp6h5093wqk5z857n3a850vqsp6u8nrxjf5fl0lxe3n488lgskdjxc6', 'balance': '1'}
{'address': 'erd14vlkclg8pthhcjgaj84ejunpnff50q42ezcvantkapcjafkx5vtq5gd2xw', 'balance': '1'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '5'}
{'address': 'erd14vlkclg8pthhcjgaj84ejunpnff50q42ezcvantkapcjafkx5vtq5gd2xw', 'balance': '1'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '3'}
{'address': 'erd14vlkclg8pthhcjgaj84ejunpnff50q42ezcvantkapcjafkx5vtq5gd2xw', 'balance': '2'}
{'address': 'erd1fam7cn6ag2zf36vflyruse2xz5azta9k506nfgkhx4taf0ry5u8s24nh7f', 'balance': '1'}
{'address': 'erd1fam7cn6ag2zf36vflyruse2xz5azta9k506nfgkhx4taf0ry5u8s24nh7f', 'balance': '1'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '4'}
{'address': 'erd1fam7cn6ag2zf36vflyruse2xz5azta9k506nfgkhx4taf0ry5u8s24nh7f', 'balance': '1'}
{'address': 'erd1fam7cn6ag2zf36vflyruse2xz5azta9k506nfgkhx4taf0ry5u8s24nh7f', 'balance': '1'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '3'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '5'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '5'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '10'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '10'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '11'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '11'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '9'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '11'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '11'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '11'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '11'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '10'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '10'}
{'address': 'erd1qqqqqqqqqqqqqpgq705fxpfrjne0tl3ece0rrspykq88mynn4kxs2cg43s', 'balance': '8'}
{'address': 'erd1s45wnqf5zw2ns62frx9fzh2znzcrcxtvlur7d8sjshkcr7em8clq64v2t9', 'balance': '1'}

It goes on but as you can see here there are multiple instances of the same address with different balances. I want to sum all of these and I have a naive method that does work but I wonder if there is a more elegant solution than making an empty dict and then using an if else statement.

This is the code I have already:

sum_wallets = {}
for wallet in r_json:
    if wallet["address"] in sum_wallets:
        sum_wallets[wallet["address"]]  = int(wallet["balance"])
    else:
        sum_wallets[wallet["address"]] = int(wallet["balance"])

CodePudding user response:

If by elegent you mean using a library then yes there is a better way using pandas

import pandas as pd
data = pd.read_json('data.json',orient='records')
summed_values = data.groupby(['address']).sum('balance')

This stores the summed values into the variable and then you can do what ever you want with it. if you want it as a dictionary then you can use

summed_values.to_dict()['balance']

CodePudding user response:

This isn't really very different to the OP's original solution although it's potentially a little more efficient:

sum_wallets = {}
for wallet in r_json:
    address = wallet['address']
    sum_wallets[address] = sum_wallets.get(address, 0)   int(wallet['balance'])
print(sum_wallets)
  • Related