dic = {'1': {'3': 0, '2': 1, '5': 1, '4': 0, '6': 29}, '3': {'1': 0, '2': 0,
    '5': 0, '4': 1, '6': 1}, '2': {'1': 13, '3': 1, '5': 21, '4': 0, '6': 0
    }, '5': {'1': 39, '3': 0, '2': 1, '4': 0, '6': 14}, '4': {'1': 1, '3': 
    1, '2': 17, '5': 2, '6': 0}, '6': {'1': 0, '3': 43, '2': 0, '5': 0, '4': 1}
    }
strs = '{0:^2} {1:^2} {2:^2} {3:^2} {4:^2} {5:^2} {6:^2}'
print(strs.format(' ', *sorted(dic)))
for x in sorted(dic):
    pass
How to make a nice matrix from a dictionary