简体   繁体   中英

Using pandas to order a dataframe alphabetically by values in one column and one row

The problem

I'm trying to order a dataframe by two different variables at the same time, I'll post a small subsample of my dataframe as an example:

+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
|                           | Caldisericumexile | Haloplasmacontractile | Chthonomonascalidirosea | Desulfurispirillumindicum | Chrysiogenesarsenatis | Gracilimonastropica | Balneolavulgaris |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| Caldisericumexile         | 0                 | 11.313708499          | 11.313708499            | 271.970586645             | 271.970586645         | 271.970586645       | 271.970586645    |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| Haloplasmacontractile     | 11.313708499      | 0                     | 11.313708499            | 271.970586645             | 271.970586645         | 271.970586645       | 271.970586645    |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| Chthonomonascalidirosea   | 11.313708499      | 11.313708499          | 0                       | 271.970586645             | 271.970586645         | 271.970586645       | 271.970586645    |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| Desulfurispirillumindicum | 271.970586645     | 271.970586645         | 271.970586645           | 0                         | 11.313708499          | 25.2982212813       | 25.2982212813    |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| Chrysiogenesarsenatis     | 271.970586645     | 271.970586645         | 271.970586645           | 11.313708499              | 0                     | 25.2982212813       | 25.2982212813    |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| Gracilimonastropica       | 271.970586645     | 271.970586645         | 271.970586645           | 25.2982212813             | 25.2982212813         | 0                   | 11.313708499     |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| Balneolavulgaris          | 271.970586645     | 271.970586645         | 271.970586645           | 25.2982212813             | 25.2982212813         | 11.313708499        | 0                |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+

Desired output

I would like to use pandas to order this alphabetically (descending) along the column with alphabetical characters and across the row with alphabetical characters, for example:

+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
|                           | Balneolavulgaris | Caldisericumexile | Chrysiogenesarsenatis | Chthonomonascalidirosea | Desulfurispirillumindicum | Gracilimonastropica | Haloplasmacontractile |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| Balneolavulgaris          | 0                | 271.970586645     | 25.2982212813         | 271.970586645           | 25.2982212813             | 11.313708499        | 271.970586645         |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| Caldisericumexile         | 271.970586645    | 0                 | 271.970586645         | 11.313708499            | 271.970586645             | 271.970586645       | 11.313708499          |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| Chrysiogenesarsenatis     | 25.2982212813    | 271.970586645     | 0                     | 271.970586645           | 11.313708499              | 25.2982212813       | 271.970586645         |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| Chthonomonascalidirosea   | 271.970586645    | 11.313708499      | 271.970586645         | 0                       | 271.970586645             | 271.970586645       | 11.313708499          |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| Desulfurispirillumindicum | 25.2982212813    | 271.970586645     | 11.313708499          | 271.970586645           | 0                         | 25.2982212813       | 271.970586645         |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| Gracilimonastropica       | 11.313708499     | 271.970586645     | 25.2982212813         | 271.970586645           | 25.2982212813             | 0                   | 271.970586645         |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| Haloplasmacontractile     | 271.970586645    | 11.313708499      | 271.970586645         | 11.313708499            | 271.970586645             | 271.970586645       | 0                     |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+

Is it possible to do this in pandas in one line with unnamed columns and rows?

Use reindex by sorted index and columns names:

df = df.reindex(index=sorted(df.index), columns=sorted(df.columns))

print (df)
                           Balneolavulgaris  Caldisericumexile  \
Balneolavulgaris                   0.000000         271.970587   
Caldisericumexile                271.970587           0.000000   
Chrysiogenesarsenatis             25.298221         271.970587   
Chthonomonascalidirosea          271.970587          11.313708   
Desulfurispirillumindicum         25.298221         271.970587   
Gracilimonastropica               11.313708         271.970587   
Haloplasmacontractile            271.970587          11.313708   

                           Chrysiogenesarsenatis  Chthonomonascalidirosea  \
Balneolavulgaris                       25.298221               271.970587   
Caldisericumexile                     271.970587                11.313708   
Chrysiogenesarsenatis                   0.000000               271.970587   
Chthonomonascalidirosea               271.970587                 0.000000   
Desulfurispirillumindicum              11.313708               271.970587   
Gracilimonastropica                    25.298221               271.970587   
Haloplasmacontractile                 271.970587                11.313708   

                           Desulfurispirillumindicum  Gracilimonastropica  \
Balneolavulgaris                           25.298221            11.313708   
Caldisericumexile                         271.970587           271.970587   
Chrysiogenesarsenatis                      11.313708            25.298221   
Chthonomonascalidirosea                   271.970587           271.970587   
Desulfurispirillumindicum                   0.000000            25.298221   
Gracilimonastropica                        25.298221             0.000000   
Haloplasmacontractile                     271.970587           271.970587   

                           Haloplasmacontractile  
Balneolavulgaris                      271.970587  
Caldisericumexile                      11.313708  
Chrysiogenesarsenatis                 271.970587  
Chthonomonascalidirosea                11.313708  
Desulfurispirillumindicum             271.970587  
Gracilimonastropica                   271.970587  
Haloplasmacontractile                   0.000000  

Try using direct setting columns (meaning [...] ) then do the same thing with .T for indexes:

>>> import pandas as pd
>>> df=pd.DataFrame({'c':[1,2,3],'a':['a','b','c'],'b':[4,5,6]},index=list('bca'))
>>> df=df[sorted(df.columns.tolist())]
>>> df=df.T[sorted(df.index.tolist())].T
>>> df
   a  b  c
a  c  6  3
b  a  4  1
c  b  5  2
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM