Python histogram from created ordinal rank

39 Views Asked by At

I am trying to create a histogram ordered by 'occupation_rank' showing the frequency distribution of data.

I've tried so many different ways, I can't see the wood from the trees any more.

Could somebody please point me in the right direction?

occupation_rank = {
  ' Adm-clerical': 5, 
  ' Exec-managerial': 13, 
  ' Handlers-cleaners': 2, 
  ' Prof-specialty': 14,
  ' Other-service': 4, 
  ' Sales': 9, 
  ' Craft-repair': 8, 
  ' Transport-moving': 7,
  ' Farming-fishing': 1, 
  ' Machine-op-inspct': 6,
  ' Tech-support':10, 
  ' Protective-serv': 11, 
  ' Armed-Forces': 12, 
  ' Priv-house-serv': 3 
}

fd_occupation = pd.Series(pd.value_counts(adult_data.occupation))
print("The frequency distribution of occupation is : ", fd_occupation.to_dict())

fd_occupation = fd_occupation.sort_index()
print(fd_occupation)

hist_fd_occupation = fd_occupation.hist(fd_occupation, bins=70)
print(hist_fd_occupation)
1

There are 1 best solutions below

1
Chris On

It doesn't sound like you are dealign with a histogram at this level at all, but already have one and just want to sort it.

As of Python 3.6+ dictionaries maintain their insertion order, so we just need to sort the pairs by their value and then turn that back into a dictionary.

>>> from operator import itemgetter
>>> occupation_rank = {
...   ' Adm-clerical': 5, 
...   ' Exec-managerial': 13, 
...   ' Handlers-cleaners': 2, 
...   ' Prof-specialty': 14,
...   ' Other-service': 4, 
...   ' Sales': 9, 
...   ' Craft-repair': 8, 
...   ' Transport-moving': 7,
...   ' Farming-fishing': 1, 
...   ' Machine-op-inspct': 6,
...   ' Tech-support':10, 
...   ' Protective-serv': 11, 
...   ' Armed-Forces': 12, 
...   ' Priv-house-serv': 3 
... }
>>> 
>>> dict(sorted(occupation_rank.items(), key=itemgetter(1)))
{' Farming-fishing': 1, ' Handlers-cleaners': 2, ' Priv-house-serv': 3, ' Other-service': 4, ' Adm-clerical': 5, ' Machine-op-inspct': 6, ' Transport-moving': 7, ' Craft-repair': 8, ' Sales': 9, ' Tech-support': 10, ' Protective-serv': 11, ' Armed-Forces': 12, ' Exec-managerial': 13, ' Prof-specialty': 14}