I am struggling with finding a way to sort the rows in a grouped pandas dataframe with two levels of indicies. This is an example of what a dataframe df I have:
I could not paste the table in a nice format, but here is code for creating the same:
import pandas as pd
data = {'Count': [207, 105, 28, 37, 182, 194]}
index = pd.MultiIndex.from_tuples([
('age_group', '20-39'),
('age_group', '40-59'),
('age_group', '<19'),
('age_group', '>60'),
('gender', 'female'),
('gender', 'male')
], names=['Parameter', 'Values'])
df = pd.DataFrame(data, index=index)
I would like to sort the level 1 age group based on this list: age_groups = ["<19", "20-39", "40-59", ">60"] in order to place the row with "<19" before "20-39".
I have a lot more parameters and values than shown here, but even with this simple example I cannot find a good solution.
df.reindex(age_groups, level=1) will remove the other Parameters. pd.MultiIndex.from_product() seems to require me to hard coded specify all other indicies which I will not do. All examples and documentation I find have the same level 1 indicies for all level 0.