https://stackoverflow.com/questions/42247379/how-to-sort-multi-level-dictionary-by-its-value
test_dict = {'Items1':{'name':'Homer', 'age':39},'Items2':{'name':'Bart', 'age':10},'Items3':{'name':'Json','age':20}}
import pandas as pd df = pd.DataFrame(test_dict) result = df.sort_values(by='age', axis=1, ascending=True).apply(OrderedDict)
Resulting with
Items2 {u'age': 10, u'name': u'Bart'} Items3 {u'age': 20, u'name': u'Json'} Items1 {u'age': 39, u'name': u'Homer'}
Other Method:
from collections import OrderedDict
test_dict = {
'Items1':{'name':'Homer', 'age':39},
'Items2':{'name':'Bart', 'age':10},
'Items3':{'name':'Json','age':20}
}
sorted_dict = OrderedDict()
sorted_keys = sorted(test_dict, key=lambda x: test_dict[x]["age"])
for key in sorted_keys:
sorted_dict[key] = test_dict[key]
print(sorted_dict)
Output:
OrderedDict([('Items2', {'age': 10, 'name': 'Bart'}), ('Items3', {'age': 20, 'name': 'Json'}), ('Items1', {'age': 39, 'name': 'Homer'})])