Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort multiIndex table based on other table

I have a multiIndex data frame like this

probe_names         PLAGL1  GRB10  MEST   H19  KCNQ1OT1  MEG3  MEG8  SNRPN  \
Patient_1        0    0.55   0.53  0.53  0.47      0.62  0.11  0.83   0.50   
                 1    0.51   0.46  0.53  0.52      0.47  0.00  0.91   0.47   
                 2     NaN    NaN   NaN  0.55       NaN   NaN   NaN    NaN   
                 3     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 4     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 0    0.54   0.59  0.53  0.47      0.66  0.13  0.90   0.51   
                 1    0.48   0.45  0.54  0.50      0.47  0.00  0.90   0.50   
                 2     NaN    NaN   NaN  0.54       NaN   NaN   NaN    NaN   
                 3     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 4     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
Patient_3        0    0.54   0.50  0.58  0.52      0.31  0.62  0.53   0.56   
                 1    0.57   0.49  0.53  0.57      0.09  0.53  0.52   0.50   
                 2     NaN    NaN   NaN  0.57       NaN   NaN   NaN    NaN   
                 3     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 4     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 0    0.49   0.49  0.53  0.48      0.27  0.58  0.53   0.53   
                 1    0.51   0.45  0.50  0.52      0.09  0.51  0.50   0.49   
                 2     NaN    NaN   NaN  0.51       NaN   NaN   NaN    NaN   
                 3     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 4     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
Patient_2        0    0.54   0.55  0.55  0.57      0.53  0.58  0.55   0.52   
                 1    0.53   0.49  0.53  0.65      0.38  0.62  0.48   0.49   
                 2     NaN    NaN   NaN  0.66       NaN   NaN   NaN    NaN   
                 3     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 4     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 0    0.51   0.53  0.55  0.62      0.52  0.57  0.53   0.50   
                 1    0.48   0.45  0.52  0.63      0.38  0.59  0.46   0.53   
                 2     NaN    NaN   NaN  0.63       NaN   NaN   NaN    NaN   
                 3     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN   
                 4     NaN    NaN   NaN   NaN       NaN   NaN   NaN    NaN  

and I have another similar table whose index is Patient_1, Patient_2 and Patient_3. I want to order my table like that. Patients are actually names and the number of patients can change depending on the input data. In the table I want to re-order, there are 10 rows per patient, in the one with the patient in the right order, each patient has 6 rows.

How can I do this? In a non-multiindex table, I normally convert the index into a list and then use that list to sort the table. But here I don’t know how to do this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

EDIT: I also have a list with this

['Patient_1', 'Patient_2', 'Patient_3']

I could use this, couldn’t I?

>Solution :

IIUC, you can simply use loc.

Here is a dummy example:

df = pd.DataFrame(np.arange(3**3).reshape(9,3),
                  columns=list('123'),
                  index=pd.MultiIndex.from_product((list('ACB'), range(3))))

      1   2   3
A 0   0   1   2
  1   3   4   5
  2   6   7   8
C 0   9  10  11
  1  12  13  14
  2  15  16  17
B 0  18  19  20
  1  21  22  23
  2  24  25  26

reordering:

l = ['A', 'B', 'C']
df.loc[l]

      1   2   3
A 0   0   1   2
  1   3   4   5
  2   6   7   8
B 0  18  19  20
  1  21  22  23
  2  24  25  26
C 0   9  10  11
  1  12  13  14
  2  15  16  17
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading