I have a pandas multiIndex dataframe that I want to order by name and number.
A similar dataset, df is created as example:
random= np.random.rand(3,10)
a = [ 'a','b','a','b','a','b','a','b','a', 'b']
b = ['p11_1','p11_1','p1_1','p1_1','p9_1','p9_1','p10_1','p10_1','p1_2','p1_2']
arrays = [a,b]
df = pd.DataFrame(data = random, columns = arrays)
Header layers look like:
a b a ... b a b
p11_1 p11_1 p1_1 ... p10_1 p1_2 p1_2
I can use the a simple sort command but that results in the wrong output:
df = df.sort_index(axis=1)
Header layers look like:
a ... b
p10_1 p11_1 p1_1 ... p1_1 p1_2 p9_1
This orders both layers, but the second layer is not the desired output.
The desired output orders the second layer as follows:
p1_1 < p1_2 < p9_1 < p10_1 < p11_1
and not as:
p10_1 < p11_1 < p1_1 < p1_2 < p9_1
Anything would help!