I am having the following dataframe:
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
The following 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 the level 0 column I getting the following:
df.columns.levels[0]
Output:
Index([u'bar', u'baz', u'foo', u'qux'], dtype='object', name=u'first')
The columns are getting sorted. Is there any way to get the level 0 column without sorting. The following way:
[u'qux', u'baz', u'foo', u'bar']
Kindly help.