Skip to content
Advertisement

Sort in nested dict by summing values python pandas

I have nested dict something like that

JavaScript

I need sort on the level “code” with depending on summing values “brands”. For example,

JavaScript

*# ‘code2.2’ first because 2+1+25=28 > 2+8+5+4=19 # ‘code3’ first because 1+2=3 > 2

I can sum values “brands” by “code” with

JavaScript

and try combine with sort function as

JavaScript

Thanks for your attention and help!

Advertisement

Answer

Try (assuming sufficient version of Python to preserve creation order of dict):

JavaScript

Prints:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement