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

奥纳洪

我有一个多索引数据框,我不想按字母顺序(axis = 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,这只是一个例子。

Quickbeam2k1

改编自熊猫文件

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')]]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在没有手动索引的情况下在Javascript中对字母等级进行排序

熊猫数据透视表手动对列进行排序

如何在 Android 中手动对 ArrayList<Model> 进行排序?

如何排序和删除多索引数据框中的列

如何在opensips中手动进行记帐

如何在RabbitMQ中手动提取队列数据?

如何在SAS EG中手动输入数据

如何在多索引数据框中添加列?

在不使用“ .insert” /不手动重新排序的情况下添加列时,如何在数据表中的特定位置插入列?

按列对数据框多索引级别进行排序

如何在数据框的列中对元组进行排序

如何使用一个顶级列对多索引熊猫数据框进行排序?

对组内多索引数据框中的聚合列进行排序

如何手动对Chrome的Smart Bar结果进行排序?

对多索引熊猫数据框进行排序

args4j:在“用法”中如何手动对选项进行排序?

如何在PHP中设置数组中的手动键值,而不是多维数组中的索引?

手动订购多索引级别

如何在多列索引中对列进行排序以在Mysql中获得最佳性能

手动创建带有日期列的数据框

如何在不手动输入 R 的情况下为多列指定 sqlSave() 的 vartype?

如何在SQL Server中手动设置Unpivot和Pivot的排序顺序

为整个数据框列手动更改 R 中“dttm”对象的小时数

如何在Eclipse PDT中为PHP项目手动触发DLTK索引

如何在pouchdb中访问手动创建的索引查找

如何在Python中手动排列Value_counts的索引

如何在多索引数据框中更改索引

同时按多列对R中的数据框进行排序

使用列中的值对熊猫数据框进行多索引