Pandas - getting unsorted hierarchical columns

I have the following data file:

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

      

Below is the output:

    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

      

When I try to get a column level 0

, I get this:

df.columns.levels[0]

      

Output:

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

      

Columns are sorted. Is there a way to get the column level 0

without sorting. The next way:

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

      

Please help.

+3


source to share


1 answer


You can use Index.get_level_values

+ Index.unique

:

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

      



Alternative solution with Index.drop_duplicates

:

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

      

+4


source







All Articles