如何在特定级别对多索引数据框列进行重新排序 [英] How can I reorder multi-indexed dataframe columns at a specific level

查看:87
本文介绍了如何在特定级别对多索引数据框列进行重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多索引的DataFrame,其名称附加到列级别.我希望能够轻松地随机排列各列,以使它们与用户指定的顺序匹配.由于此操作正在进行中,因此我无法使用此推荐的解决方案并在创建时正确排序.

I have a multi-indexed DataFrame with names attached to the column levels. I'd like to be able to easily shuffle the columns around so that they match the order specified by the user. Since this is down the pipeline, I'm not able to use this recommended solution and order them properly at creation time.

我有一个看起来(类似)的数据表

I have a data table that looks (something) like

Experiment           BASE           IWWGCW         IWWGDW
Lead Time                24     48      24     48      24     48
2010-11-27 12:00:00   0.997  0.991   0.998  0.990   0.998  0.990
2010-11-28 12:00:00   0.998  0.987   0.997  0.990   0.997  0.990
2010-11-29 12:00:00   0.997  0.992   0.997  0.992   0.997  0.992
2010-11-30 12:00:00   0.997  0.987   0.997  0.987   0.997  0.987
2010-12-01 12:00:00   0.996  0.986   0.996  0.986   0.996  0.986

我想输入像['IWWGCW', 'IWWGDW', 'BASE']这样的列表,并将其重新排序为:

I want to take in a list like ['IWWGCW', 'IWWGDW', 'BASE'] and reorder this to be:

Experiment           IWWGCW         IWWGDW         BASE           
Lead Time                24     48      24     48      24     48  
2010-11-27 12:00:00   0.998  0.990   0.998  0.990   0.997  0.991  
2010-11-28 12:00:00   0.997  0.990   0.997  0.990   0.998  0.987  
2010-11-29 12:00:00   0.997  0.992   0.997  0.992   0.997  0.992  
2010-11-30 12:00:00   0.997  0.987   0.997  0.987   0.997  0.987  
2010-12-01 12:00:00   0.996  0.986   0.996  0.986   0.996  0.986  

我并不总是知道实验"将处于什么级别.我尝试过(其中df是上面显示的多索引框架)

with the caveat that I don't always know at what level "Experiment" will be. I tried (where df is the multi-indexed frame shown above)

df2 = df.reindex_axis(['IWWGCW', 'IWWGDW', 'BASE'], axis=1, level='Experiment')

但这似乎不起作用-它成功完成,但是返回的DataFrame的列顺序保持不变.

but that didn't seem to work - it completed successfully, but the DataFrame that was returned had its column order unchanged.

我的解决方法是具有以下功能:

My workaround is to have a function like:

def reorder_columns(frame, column_name, new_order):
    """Shuffle the specified columns of the frame to match new_order."""

    index_level  = frame.columns.names.index(column_name)
    new_position = lambda t: new_order.index(t[index_level])
    new_index    = sorted(frame.columns, key=new_position)
    new_frame    = frame.reindex_axis(new_index, axis=1)
    return new_frame

其中reorder_columns(df, 'Experiment', ['IWWGCW', 'IWWGDW', 'BASE'])符合我的期望,但感觉像是我在做额外的工作.有没有更简单的方法可以做到这一点?

where reorder_columns(df, 'Experiment', ['IWWGCW', 'IWWGDW', 'BASE']) does what I expect but it feels like I'm doing extra work. Is there an easier way to do this?

推荐答案

我暂时不知道有什么东西.为此创建了一个增强票证:

I don't know of anything off-hand. Created an enhancement ticket about it:

http://github.com/pydata/pandas/issues/1864

这篇关于如何在特定级别对多索引数据框列进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆