pandas - 获取未排序的分层列

时间:2017-05-23 16:20:58

标签: python pandas

我有以下数据框:

import numpy as np
import pandas as pd
arrays = [['qux', 'qux', 'baz', 'baz', 'foo', 'foo', 'bar', 'bar'],
['one', 'two', 'one', 'two', 'one', 'two', 'one', 'two']]
tuples = zip(*arrays)
index = pd.MultiIndex.from_tuples(tuples)
df = pd.DataFrame(np.random.randn(3, 8), index=['A', 'B', 'C'], columns=index)
print df

以下是输出:

    qux                     baz                     foo                     bar
    one         two         one         two         one         two         one         two
A   0.504208    1.059471    1.488488    0.807279    0.788239    0.110510    0.882414    0.120483
B   0.178940    0.099793    0.460812    -1.388569   1.264663    -0.050531   -0.839683   0.472138
C   0.356101    -0.172082   0.859077    -0.560092   0.450147    1.200750    -0.433077   0.437339

当我尝试获取level 0列时,我得到以下内容:

df.columns.levels[0]

输出:

Index([u'bar', u'baz', u'foo', u'qux'], dtype='object', name=u'first')

列正在排序。有没有办法在没有排序的情况下获取level 0列。以下方式:

[u'qux', u'baz', u'foo', u'bar']

请帮助。

1 个答案:

答案 0 :(得分:4)

您可以使用Index.get_level_values + Index.unique

print (df.columns.get_level_values(0).unique())
Index(['qux', 'baz', 'foo', 'bar'], dtype='object')

使用Index.drop_duplicates的替代解决方案:

print (df.columns.get_level_values(0).drop_duplicates())
Index(['qux', 'baz', 'foo', 'bar'], dtype='object')