问题
我试图同时按两个不同的变量对数据框进行排序,我将以一个较小的子数据示例作为示例:
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
| | 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 |
+---------------------------+-------------------+-----------------------+-------------------------+---------------------------+-----------------------+---------------------+------------------+
所需的输出
我想用大熊猫沿着带有字母字符的列和沿着字母字符的行按字母顺序(降序)排序,例如:
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
| | 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 |
+---------------------------+------------------+-------------------+-----------------------+-------------------------+---------------------------+---------------------+-----------------------+
是否可以在一行未命名的列和行的大熊猫中做到这一点?
答案 0 :(得分:1)
通过排序的索引和列名称使用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
答案 1 :(得分:1)
尝试使用直接设置列(表示[...]
),然后对索引使用.T
做同样的事情:
>>> 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
>>>