0

My dataframe looks like this:

Method Dataset          
A1     B2        10   20
       B3        10   20
       B1        10   20
       B1        10   20
A2     B2        10   20
       B1        10   20
A3     B9        10   20
       B5        10   20

The Dataset index is a string. How can I sort just the second (Dataset) index using a list like ["B1", "B2", "B3", "B4", "B5"] ? I think I'm looking for sortindex() but with custom ordering.

Milad
  • 4,901
  • 5
  • 32
  • 43
  • 2
    Try `df.sort_index(level=1)` – cs95 Jun 10 '20 at 17:01
  • I think `sort_index` works on string index by alphanumerical order, and your list seems to follow this order, is it what you want? or I`m missing something – Ben.T Jun 10 '20 at 17:01
  • Oh that was just an example. I do want a very specific sorting that's not alphanumerical. – Milad Jun 10 '20 at 17:16
  • 1
    The MultiIndex might complicate things a bit, but there are several options here: https://stackoverflow.com/questions/23482668/sorting-by-a-custom-list-in-pandas, a categorical dtype might be the simplest. – ALollz Jun 10 '20 at 17:26

0 Answers0