Try this method -
- Sort the
education_rank
as a series to get index values
- Use index values to fetch rows from the
value_counts
series
Dropna
if any
#Your predefined rankings
education_rank = {'Bachelors':12, 'HS-grad':8, '11th':6, 'Masters':14, '12th':77}
#Your frequency output from value_counts()
fd_education = pd.Series({'Bachelors':500, 'HS-grad':809, '11th':23, 'Masters':65})
fd_education[pd.Series(education_rank).sort_values().index].dropna()
11th 23
HS-grad 809
Bachelors 500
Masters 65
dtype: int64
Explanation -
The issue is that you are passing a dictionary to the level instead of the index name of the series object. The goal of level to help with multi-index situations. This lets it decide which of the indexes to sort on. You cant provide sequence as a list/dict to sort on.
If it is unable to find the index name you have provided, it will just resort to sorting by alphabetical order. Check this example -
#Your predefined rankings
education_rank = {'Bachelors':12, 'HS-grad':8, '11th':6, 'Masters':14, '12th':77}
#Your frequency output from value_counts()
fd_education = pd.Series({'Bachelors':500, 'HS-grad':809, '11th':23, 'Masters':65})
fd_education = fd_education.sort_index(level='hello') #<----
print(fd_education)
11th 23
Bachelors 500
HS-grad 809
Masters 65
dtype: int64
Do read documentation for more details.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…