python - 使用 pandas 按一列一行中的值按字母顺序对数据框进行排序

标签 python pandas

问题

我试图同时通过两个不同的变量来订购数据帧,我将发布我的数据帧的一个小子样本作为示例:

+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
|                           | 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                |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+

所需输出

我想使用 pandas 沿包含字母字符的列和包含字母字符的行按字母顺序(降序)排序,例如:

+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
|                           | 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                     |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+

是否可以在 pandas 中使用未命名的列和行在一行中执行此操作?

最佳答案

使用reindex按排序索引和列名称:

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  

关于python - 使用 pandas 按一列一行中的值按字母顺序对数据框进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983216/

相关文章:

python - 在python中将不同工作表中的列合并到单个工作表中

python - df1 中不在 df2 中的所有行

python - 如何在 Visual Studio Code 中设置 Python 虚拟环境?

python - Python 3 中列表操作从左到右的应用

python - Beautifulsoup 跨度 id 标签到 Pandas

python - 使用 Groupby 时调用具有多个参数的函数

python-3.x - 为什么 pandas 自己将 dataframe 中的 int 值转换为 float 值?

python - 是否有一种可接受的方法让函数从参数中弹出一个值?

python - pylibmc : undefined symbol: memcached_server_list

python - 将 Pandas 数据帧转换为邻接矩阵