根据参考对多索引数据框列进行重新排序 [英] Reorder Multi-indexed dataframe columns based on reference
本文介绍了根据参考对多索引数据框列进行重新排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个多索引数据框,其名称附加到列级别.数据表如下所示:(df1)
I have a multi-indexed dataframe with names attached to the column levels. The data table looks something like this: (df1)
TIME
TMC 111N1 111P2 111N3 111P4
DATE EPOCH
0 143 113 103 NaN
1 183 NaN NaN NaN
2 NaN NaN NaN NaN
3 143 NaN NaN NaN
我想对列进行改组,以使其与参考数据帧(df2)的行索引指定的顺序匹配:
I'd like to shuffle the columns around so that they match the order specified by the rows index of a reference dataframe (df2):
A1 A2 A3 A4 A5
Name
111N3 PA PL er 0.75543 35
111P4 PA PL er 0.09413 35
111N1 PA PL er 4.21557 35
111P2 PA PL er 1.31989 35
即结果应该是(df3):
i.e. the result should be (df3):
TIME
TMC 111N3 111P4 111N1 111P2
DATE EPOCH
0 103 NaN 143 113
1 NaN NaN 183 NaN
2 NaN NaN NaN NaN
3 NaN NaN 143 NaN
推荐答案
reindex_axis
将使用其他数据框中的标签,并允许您指定要重新索引的轴以及特定级别:
reindex_axis
will use the labels from the other dataframe and let you specific the axis to reindex and also a particular level:
df1.reindex_axis(df2.index, axis=1, level=1)
这篇关于根据参考对多索引数据框列进行重新排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文