python - 如何手动对多索引数据框中的列进行排序?

标签 python pandas multi-index

我有一个多索引数据框,我想不按字母顺序(轴=1)对列进行排序,而是按自定义顺序。 我使用 unstack 来将 df 转换为多索引,并使用 sort_index 进行排序:

df = df.unstack().swaplevel(1,0, axis=1).sort_index(axis=1, level=0)

我希望我的指标列按照我的意愿排序,而不是按字母顺序排序,例如:椅子索引和表格索引(以及更多)中的 metric2、metric3、metric1。

dim3          chair            table
              metric1 metric2   metric3 metric1 metric2 metric3
dim1    dim2                        
a       day1    1.0   10.0      123.0    NaN     NaN     NaN
b       day2    NaN   NaN       NaN 2.0  20.0    456.0

请不要介意 null,这只是一个示例。

最佳答案

改编自 pandas documentation

import pandas as pd
import numpy as np
arrays = [['bar', 'bar', 'baz', 'baz', 'foo', 'foo', 'qux', 'qux'],
          ['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = list(zip(*arrays))
index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second']) 
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
df
first        bar                 baz                 foo                 qux  \
second       one       two       one       two       one       two       one   
A       0.033707  0.681401 -0.999368 -0.015942 -0.417583 -0.233212 -0.072706   
B       1.140347 -0.759089 -0.278175 -0.848010 -0.642824 -0.902858  0.117839   
C      -0.370039 -0.425074 -0.404409 -1.090386 -0.985019 -0.971178  0.924350   

first             
second       two  
A      -0.850698  
B       0.377443  
C      -1.129125  

现在检查

df.columns.tolist()
[('bar', 'one'),
 ('bar', 'two'),
 ('baz', 'one'),
 ('baz', 'two'),
 ('foo', 'one'),
 ('foo', 'two'),
 ('qux', 'one'),
 ('qux', 'two')]

根据您的喜好重新排列并使用.loc

df.loc[:,[('bar', 'one'),
('baz', 'one'),
 ('bar', 'two'),
('foo', 'one'),
 ('foo', 'two'),
 ('qux', 'two'),
 ('baz', 'two'),
 ('qux', 'one')
] ]
first        bar       baz       bar       foo                 qux       baz  \
second       one       one       two       one       two       two       two   
A       0.033707 -0.999368  0.681401 -0.417583 -0.233212 -0.850698 -0.015942   
B       1.140347 -0.278175 -0.759089 -0.642824 -0.902858  0.377443 -0.848010   
C      -0.370039 -0.404409 -0.425074 -0.985019 -0.971178 -1.129125 -1.090386   

first        qux  
second       one  
A      -0.072706  
B       0.117839  
C       0.924350

这种方法应该为您提供最大程度的控制。

使这种方法适应您的数据框,它看起来像这样:

df = df.unstack().swaplevel(1,0, axis=1).loc[:, [('chair', 'metric2'),
        ('chair', 'metric3'), ('chair', 'metric1'),('table', 'metric2'),
        ('table', 'metric3'), ('table', 'metric1')]]

关于python - 如何手动对多索引数据框中的列进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57738004/

相关文章:

python - Django - 删除对象,保持 parent ?

python - 如何在同一张图中绘制多个线性回归

python - 如何为我的数据集创建类似直方图的条形图?

python - 多索引 pandas 系列中的 set_codes

python - 如何将每个列乘以另一个 df 的所有列,获得多重索引?

python - 在 Python 中将 3 个单独的 numpy 数组组合为 RGB 图像

python - 如何为 QTableView 列设置标题标签?

python - 如何使用Python创建一个非常简单的DNS服务器?

python - 删除数据框中的重复项,同时保留最旧的记录

Python 多索引 : Finding cordinates with level 2 index, DataFrame